How can I write to stdout from Python and feed it simultaneously (via a Unix pipe) to another program? For example if you have
# write file line by line
with open("myfile") as f:
for line in f:
print line.strip()
But you’d like that to go line by line to another program, e.g. | wc -l so that it outputs the lines in myfile. How can that be done? thanks.
If you want to pipe
pythontowcexternally, that’s easy, and will just work:If you want to tee it so its output both gets printed and gets piped to
wc, tryman teeor, better, your shell’s built-in fancy redirection features.If you’re looking to run
wc -lfrom within the script, and send your output to both stdout and it, you can do that too.First, use
subprocess.Popento startwc -l:Now, you can just do this:
That will have
wc‘s output go to the same place as your script’s. If that’s not what you want, you can also make itsstdouta PIPE. In that case, you want to usecommunicateinstead of trying to get all the fiddly details right manually.