问题描述
我要自动升级程序.
我在Python中运行以下代码:
I run in Python this code:
import subprocess
subprocess.call('./upgrade')
执行此操作时,我从外壳程序获得输出,表明升级过程已成功启动,然后出现按Enter继续"的信息.我将如何自动执行此过程,以便在提示时python脚本自动按"回车?我需要在程序执行两次.我需要在Linux而不是Windows上完成此操作,因为在这里被问到:生成键盘事件另外,需要在Shell提示输入Enter之后专门执行此操作.谢谢你的帮助.我在这里找不到解决方案:按Enter作为命令输入
When I do this, I get output from shell that Upgrade procedure started successfully, and then I get 'Press Enter to continue'. How would I automatize this process, so that python script automatically "presses" enter when promted? I need this to be done twice during procedure.I need this to be done on Linux, not Windows, as it was asked here:Generate keyboard eventsAlso, this needs to be done specifically after Shell prompts for Enter.Thanks for any help.I did not find solution here:Press enter as command input
推荐答案
您可以使用subprocess.Popen
和subprocess.communicate
将输入发送到另一个程序.
You can use subprocess.Popen
and subprocess.communicate
to send input to another program.
例如,将"enter"键发送到以下程序test_enter.py
:
For example, to send "enter" key to the following program 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" 也很有帮助.
You may find answer to "how do i write to a python subprocess' stdin" helpful as well.
这篇关于在Shell上提示时如何使python脚本按'enter'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!