I am calling a perl script on an external txt files from python, and printing the output to an outfile. But instead I want to pipe the output to unix's sort. Right now I am not piping, but are writing the output from the perl program first, then doing by combining my code under, and this stackoverflow answer.
import subprocess
import sys
import os
for file in os.listdir("."):
with open(file + ".out", 'w') as outfile:
p = subprocess.Popen(["perl", "pydyn.pl", file], stdout=outfile)
p.wait()
Since you asked the question in python you can also pipe the result
p = subprocess.Popen("perl pydyn.pl %s | sort" % file, stdout=outfile,shell=True)
but for this you're gonna have to make it shell=True
which is not a good practice
Here's one way without making it shell=True
p = subprocess.Popen(["perl", "pydyn.pl", file], stdout=subprocess.PIPE)
output = subprocess.check_output(['sort'], stdin=p.stdout,stdout=outfile)
p.wait()