我有一个从命令行运行的程序,如下所示:
$ program a.txt b.txt
该程序需要两个文本文件作为参数。我正在尝试编写一个 Python 3.2 脚本来运行上述程序。我怎样才能做到这一点?目前,我正在尝试使用subprocess
像这样的模块:
import subprocess
with open("a.txt", mode="r") as file_1:
with open("b.txt", mode="r") as file_2:
cmd = ['/Users/me/src/program', file_1, file_2]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
for line in process.stdout:
print(line)
I read 这个帖子 https://stackoverflow.com/questions/89228/how-to-call-external-command-in-python和帖子here https://stackoverflow.com/questions/325463/launch-a-shell-command-with-in-a-python-script-wait-for-the-termination-and-retu,这似乎描述了我的问题的类似解决方案。不幸的是,读完这些文章后,我似乎仍然无法让我的 Python 代码运行我的程序。
有人可以帮忙吗?提前致谢!
Look at @克里斯的回答 https://stackoverflow.com/questions/6945466/using-python-to-run-another-program/6945501#6945501, 并且:
子进程不会等待命令完成,因此您应该使用wait
method.
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
process.wait()
for line in process.stdout:
print(line)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)