how to interact with an external script(program)
there is a script that expects keyboard input, i can call that script with os.system('./script') in python,
how is it possible to send back an input to the script from another calling script?
update:
the script is:
$ c开发者_Python百科at script
#!/usr/bin/python
for i in range(4):
name=raw_input('enter your name')
print 'Welcome %s :) ' % name
when i try without a for loop, it works but it shows the output only when the script quits.
>>> p = subprocess.Popen('./script',stdin=subprocess.PIPE)
>>> p.communicate('navras')
enter your nameWelcome navras :)
when i try it with the foor loop, it throws error, How to display the statements interactive as and when the stdout is updated with new print statements
>>> p.communicate('megna')
enter your nameWelcome megna :)
enter your nameTraceback (most recent call last):
File "./script", line 3, in <module>
name=raw_input('enter your name')
EOFError: EOF when reading a line
(None, None)
You can use subprocess instead of os.system:
p = subprocess.Popen('./script',stdin=subprocess.PIPE)
p.communicate('command')
its not testet
In fact, os.system and os.popen are now deprecated and subprocess is the recommended way to handle all sub process interaction.
精彩评论