I'm calling a python script (B) from another python script (A).
Using subprocess.call, how do I redirect the stdout of B to a file that specify?
I'm using python 2.6.1.
Pass a file as the stdout parameter to subprocess.call:
with open('out-file.txt', 'w') as f:
subprocess.call(['program'], stdout=f)
stderr=f.Alternate approach
import subprocess
p=subprocess.Popen('lsblk -l|tee a.txt',stdout=subprocess.PIPE,shell=True)
(output,err)=p.communicate()
p_status=p.wait()
print output
Above code will write output of command to file a.txt
shell=True; you can have two separate Popen objects, one for lsblk and a second one for tee, with the first's stdout connected to the second's stdin; the subprocess module docs show how to do so.
subprocess.call()to execute another Python script? Wouldn't it be better toimportit and call the appropriate functions?