Here is my code.
import subprocess
bashCommand = "./program -s file_to_read.txt | ./awk_program.txt"
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
output2 = process.stdout
print output
print output2
This bash command alone when used in terminal prints the output of the awk_program (which just prints to stdout). But in python, output prints nothing and output2 prints
<closed file '<fdopen>', mode 'rb' at 0x2b5b20>
What do I need to do to return the output?
You need to use the option
shell=TrueinPopen()to have pipes work.Note that if you don’t know exactly where the
Popeninput is coming from, thenshell=Trueis a security risk.Also, you don’t need to split
bashCommandhere. For example: