I'm trying to run a Perl script from Python. I know that if run the Perl script in terminal and I want the output of the Perl script to be written a file I need to add > results.txt
after perl myCode.pl
. This works fine in the terminal, but when I try to do this in Python it doesn't work.
This the code:
import shlex
import subprocess
args_str = "perl myCode.pl > results.txt"
args = shlex.split(args_str)
subprocess.call(args)
Despite the > results.txt
it does not output to that file but it does output to the command line.
or
or
The first two invoke a shell to execute the shell command
perl myCode.pl > results.txt
. The last one executesperl
directly by havingcall
do the redirection itself. This is the more reliable solution.