您可以使用subprocess.Popen
和subprocess.communicate
将输入发送到另一个程序。
例如,将“ enter”键发送到以下程序test_enter.py
:
print "press enter..."
raw_input()
print "yay!"
您可以执行以下操作:
from subprocess import Popen, PIPE
p = Popen(['python test_enter.py'], stdin=PIPE, shell=True)
p.communicate(input='\n')
您可能还会发现“如何编写python子进程的stdin”的答案也很有帮助。