获取`perl file.pl<的输出some_file.txt` shell命令

时间:2015-11-10 17:37:25

标签: python subprocess

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

的输出

怎么做?

1 个答案:

答案 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>