您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何从Python脚本调用可执行文件?

如何从Python脚本调用可执行文件?

要执行外部程序,请执行以下操作:

import subprocess
args = ("bin/bar", "-c", "somefile.xml", "-d", "text.txt", "-r", "aString", "-f", "anotherString")
#Or just:
#args = "bin/bar -c somefile.xml -d text.txt -r aString -f anotherString".split()
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read()
print output

是的,假设您的bin/bar程序将其他一些分类文件写入磁盘,则可以使用正常打开它们open("path/to/output/file.txt")。请注意,如果不需要,您不需要依赖子外壳将输出重定向到磁盘上名为“输出”的文件。我在这里展示如何直接将输出读取到您的python程序中而无需在其间插入磁盘。

python 2022/1/1 18:16:42 有553人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶