在python脚本中处理perl脚本

时间:2015-06-25 07:29:05

标签: python perl subprocess

我试图在另一个python脚本中执行perl脚本。我的代码如下:

command = "/path/to/perl/script/" + "script.pl"
input = "< " + "/path/to/file1/" + sys.argv[1] + " >"
output = "/path/to/file2/" + sys.argv[1]

subprocess.Popen(["perl", command, "/path/to/file1/", input, output])

执行python脚本时,它返回:

No info key.

导致perl脚本的所有路径以及文件都是正确的。

我的perl脚本使用命令执行:

perl script.pl /path/to/file1/ < input > output

对此的任何建议都非常感谢。

1 个答案:

答案 0 :(得分:2)

shell命令的模拟:

#!/usr/bin/env python
from subprocess import check_call

check_call("perl script.pl /path/to/file1/ < input > output", shell=True)

是:

#!/usr/bin/env python
from subprocess import check_call

with open('input', 'rb', 0) as input_file, \
     open('output', 'wb', 0) as output_file:
    check_call(["perl", "script.pl", "/path/to/file1/"],
               stdin=input_file, stdout=output_file)

要避免使用详细代码,您可以use plumbum to emulate a shell pipeline

#!/usr/bin/env python
from plumbum.cmd import perl $ pip install plumbum

((perl["script.pl", "/path/to/file1"] < "input") > "output")()

注意:只有shell=True的代码示例运行shell。第2和第3个示例不使用shell。

相关问题