Python 获取“perl file.pl”的输出<;some_file.txt`shell命令
这个文件Python 获取“perl file.pl”的输出<;some_file.txt`shell命令,python,subprocess,Python,Subprocess,这个文件无法打开perl脚本“file.pl”:没有这样的文件或目录。python脚本和perl文件位于同一目录中 我希望line成为perl file.pl
无法打开perl脚本“file.pl”:没有这样的文件或目录
。python脚本和perl文件位于同一目录中
我希望line
成为perl file.pl
如何做到这一点
我希望该行是perl file.pl
要修复
无法打开perl脚本“file.pl”:没有这样的文件或目录
,请将完整路径传递到file.pl
,例如,如果它与Python脚本位于同一目录中,则可以使用获取路径。是当前工作目录中的file.pl吗?@ChadS.Yes,它位于同一目录中。我已更新了问题。您的问题似乎与子流程
和cStringIO
无关。请尝试指定file.pl
的绝对路径。它位于同一目录中,实际上不会回答@ChadS。关于其是否在当前工作目录中的问题。您可以尝试os.path.join(os.path.dirname(\uuuuu file\uuuu),'file.pl')
来显式查找脚本的目录。为什么要使用shell=True
?我认为这里不需要它。如果不确定,请不要使用shell=True。
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()
#!/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)