press enter via python script

家住魔仙堡 提交于 2019-12-08 05:24:59

问题


We are trying to create a python script to install an app via windows shell prompt, executing our python script. We have an output prompt from the app.exe indicating "Press Enter to Continue..."

We tried to simulate the Enter key but it doesn't work. The prompt just sits still not moving to the next wizard step.

How do we overcome this problem?

import subprocess
import win32console

APP_BIN = 'app.exe'

def main():
    proc = subprocess.Popen([APP_BIN,'-i','console'],stdin=subprocess.PIPE,
                                                     stdout=subprocess.PIPE)
    proc.stdin.write("\r\n")            <--- issue
    output = proc.stdout.readline()     <--- issue
    print output
    ret = proc.wait()
    print ret

if __name__ == '__main__':
    main()

回答1:


Not entirely sure how to do it in python, but my suggestion would be simulate an actual 'enter' key press command. In your code, you are just changing caret's position and not issuing a proper return.

Have a look at this: http://win32com.goermezer.de/content/view/136/254/

import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("{ENTER}", 0) 

Seems like that's exactly what you need.




回答2:


The following may work (untested):

import subprocess
import win32console

APP_BIN = 'app.exe'

def main():
    proc = subprocess.Popen([APP_BIN,'-i','console'],stdin=subprocess.PIPE,
                                                     stdout=subprocess.PIPE)
    stdoutdata, stderrdata = proc.communicate(input="\r\n")
    output = stdoutdata.readline()
    print output
    ret = proc.wait()
    print ret

if __name__ == '__main__':
    main()


来源:https://stackoverflow.com/questions/15100977/press-enter-via-python-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!