【发布时间】:2015-03-11 19:07:25
【问题描述】:
我正在从 python 调用外部 txt 文件的 perl 脚本,并将输出打印到输出文件。但相反,我想将输出通过管道传输到 unix 的排序。现在我不是管道,而是首先编写 perl 程序的输出,然后将我的代码组合在 this stackoverflow answer 下。
import subprocess
import sys
import os
for file in os.listdir("."):
with open(file + ".out", 'w') as outfile:
p = subprocess.Popen(["perl", "pydyn.pl", file], stdout=outfile)
p.wait()
【问题讨论】:
-
您使用的 python 不正确。如果你要调用很多外部进程(比如排序),而不是使用 python 模块,最好使用 bash、ipython 或其他 shell。
-
您也可以通过管道输出结果
p = subprocess.Popen("perl pydyn.pl %s | sort" % file, stdout=outfile,shell=True),但为此您必须使用shell=True这不是一个好习惯
标签: python subprocess piping