Here is how you can catch the output of the first process and pass it to the second, which will then write its output to the file:
import subprocess with open('CONTENT','w') as f1: p1 = subprocess.Popen(["sort", "CONTENT1"], stdout=subprocess.PIPE) p2 = subprocess.Popen(["uniq"], stdin=subprocess.PIPE, stdout=f1) p1_out = p1.communicate()[0] # catch output p2.communicate(p1_out) # pass input
You should not tinker with sys.stdout at all. Note that you need one call to the method communicate for each process. Note also that communicate() will buffer all output of p1 before it is passed to p2.
Here is how you can pass the output of p1 line-by-line to p2:
import subprocess with open('CONTENT','w') as f1: p1 = subprocess.Popen(["sort", "CONTENT1"], stdout=subprocess.PIPE) p2 = subprocess.Popen(["uniq"], stdin=subprocess.PIPE, stdout=f1) out_line = p1.stdout.readline() while out_line: p2.stdin.write(out_line) out_line = p1.stdout.readline()
The cleanest way to do the pipe would be the following:
import subprocess with open('CONTENT','w') as f1: p1 = subprocess.Popen(["sort", "CONTENT1"], stdout=subprocess.PIPE) p2 = subprocess.Popen(["uniq"], stdin=p1.stdout, stdout=f1) p1.stdout.close()
Alternatively, of course, you could just use the facilities of the shell, which is just made for these tasks:
import subprocess with open('CONTENT','w') as f1: p = subprocess.Popen("sort CONTENT1 | uniq", shell=True, stdout=f1)
Reference: http://docs.python.org/2/library/subprocess.html
p2stdout directly tof1?stdout=f1.