Reputation: 47
I'm trying to execute a perl script within another python script. My code is as below:
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])
When execute the python script, it returned:
No info key.
All path leading to the perl script as well as files are correct.
My perl script is executed with command:
perl script.pl /path/to/file1/ < input > output
Any advice on this is much appreciate.
Upvotes: 0
Views: 1019
Reputation: 414865
The analog of the shell command:
#!/usr/bin/env python
from subprocess import check_call
check_call("perl script.pl /path/to/file1/ < input > output", shell=True)
is:
#!/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)
To avoid the verbose code, you could 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")()
Note: Only the code example with shell=True
runs the shell. The 2nd and 3rd examples do not use shell.
Upvotes: 2