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
怎么做?
答案 0 :(得分:0)
我希望line成为
的输出perl file.pl < some_file.txt
#!/usr/bin/env python
from subprocess import check_output
with open('some_file.txt', 'rb', 0) as file:
line = check_output(['perl', 'file.pl'], stdin=file)
要修复Can't open perl script "file.pl": No such file or directory
,请将完整路径传递给file.pl
,例如,如果它与Python脚本位于同一目录中,则可以使用get_script_dir()
function来获取路径。< / p>