Skip to content

Instantly share code, notes, and snippets.

@naftulikay
Created May 30, 2016 23:27
Show Gist options
  • Save naftulikay/2d99cb93f078a01369fdd6e4cde4af90 to your computer and use it in GitHub Desktop.
Save naftulikay/2d99cb93f078a01369fdd6e4cde4af90 to your computer and use it in GitHub Desktop.
Pipe Two Programs together in Python
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import argparse
import subprocess
def __main__():
parser = argparse.ArgumentParser(prog="piper", description="Pipes output of two programs together.")
parser.parse_args()
p_ls = subprocess.Popen(['ls', '-lh'], stdout=subprocess.PIPE)
p_wc = subprocess.Popen(['wc', '-c'], stdin=p_ls.stdout, stdout=subprocess.PIPE)
# allow p_ls to receive a SIGPIPE if p_wc exits (ie wc failed)
p_ls.stdout.close()
# await results of the pipe
output = p_wc.communicate()[0]
# sample output:
# ls:None, wc:0, output:1118
print("ls:{ls_return}, wc:{wc_return}, output:{wc_output}".format(
ls_return=p_ls.returncode, wc_return=p_wc.returncode,
wc_output=output.strip()))
if __name__ == "__main__":
__main__()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment