【发布时间】:2016-06-15 14:00:46
【问题描述】:
我想调用一个exe文件并且已经输入了参数/输入数据。
cmd = dir_path + 'file.exe do something test'
p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
这已经很好了。如果您使用 exe 文件执行此操作,您将按 Enter 键执行下一步,即输入您的用户名。然后密码等等。现在我不知道如何在我的脚本中实现这一点,这些步骤都是我的脚本完成的。基本上我不知道如何使用 python 执行 ENTER 步骤。感谢您的帮助。
更新
现在尝试了 2 种方法(在这个问题中或在 stackoverflow 上都提到过)。
方式 1>
p = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, universal_newlines=True)
# cmd represents the path of the exe file
p.stdin.write(username+"\n")
p.stdin.write(password+"\n")
p.sdin.close()
方式 2
p = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, universal_newlines=True)
out,err = p.communicate("\n".join([username,password,"\n"]))
# last \n should terminate the exe file and communicate should wait for the process to exit
两种方式都在一行中显示相同的输出:
用户名:密码:
通常我希望是这样的:
用户名:“进程写入的用户名+\n”
密码:“进程写入的密码+\n”
应用程序在 windows7 上运行(必须是),exe 文件在命令行中运行。
那么有没有可能这个exe不支持way1和way2所以它不可能通过子进程来写或者我犯了一个重大错误?!
再次感谢您的帮助。
【问题讨论】:
-
搜索
PyAutoIt -
看起来不错,但我无法在我的工作电脑上安装软件或库。有子流程或类似的解决方案吗?
-
你平时是怎么输入用户名、密码的?它是一个 GUI 应用程序吗?注意:the application may read/write directly to the console outside of its standard input/output streams 即
stdin=PIPE可能无济于事。你的操作系统是什么?在 Unix 上,你可以usepexpectand here's why。
标签: python pipe subprocess popen