You can use shell=True
in subprocess.call
However, a (much) better way to do this would be:
command = ['program',arg1]
with open('temp.txt','w') as fout:
subprocess.call(command,stdout=fout)
This removes the shell from the whole thing making it more system independent, and it also makes your program safe from "shell injection" attacks (consider arg1='argument; rm -rf ~'
or whatever the windows equivalent is).
The context manager (with
statement) is a good idea as it guarantees that your file object is properly flushed and closed when you leave the "context".
Note that it is important that if you're not using shell=True
to a subprocess.Popen
(or similar) class, you should pass the arguments as a list, not a string. Your code will be more robust that way. If you want to use a string, python provides a convenience function shlex.split
to split a string into arguments the same way your shell would. e.g.:
import subprocess
import shlex
with open('temp.txt','w') as fout:
cmd = shlex.split('command argument1 argument2 "quoted argument3"'
#cmd = ['command', 'argument1', 'argument2', 'quoted argument3']
subprocess.call(cmd,stdout=fout)