要执行外部程序,请执行以下操作:
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程序中而无需在其间插入磁盘。