Created
May 30, 2016 23:27
-
-
Save naftulikay/2d99cb93f078a01369fdd6e4cde4af90 to your computer and use it in GitHub Desktop.
Pipe Two Programs together in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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