【发布时间】:2015-11-10 17:37:24
【问题描述】:
output = cStringIO.StringIO()
output.write('string') # this emulates some_file.txt
p = subprocess.Popen(['perl', 'file.pl'], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
line, _ = p.communicate(output.getvalue())
print line
output.close()
这会打印出Can't open perl script "file.pl": No such file or directory。 python脚本和perl文件在同一目录下!
我希望line 成为perl file.pl < some_file.txt 的输出
如何做到这一点?
【问题讨论】:
-
file.pl 在当前工作目录中吗?
-
@ChadS.是的,它在同一个目录中。我已经更新了问题。
-
您的问题似乎与
subprocess和cStringIO无关。尝试指定file.pl的绝对路径。 -
它在同一个目录中并没有真正回答@ChadS。关于它是否在当前工作目录中的问题。您可以尝试
os.path.join(os.path.dirname(__file__), 'file.pl')明确查看脚本目录。 -
你为什么使用
shell=True?我认为这里不需要。如果不确定,请不要使用 shell=True。
标签: python subprocess