Created
July 29, 2015 17:05
-
-
Save hnakamur/86e05336b94bcc1da7a4 to your computer and use it in GitHub Desktop.
multiple commands pipe 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
from subprocess import Popen, PIPE | |
cmd = 'cat | head -5' | |
proc = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) | |
outs, errs = proc.communicate(input=""" | |
Popen.communicate(input=None, timeout=None) | |
Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be data to be sent to the child process, or None, if no data should be sent to the child. The type of input must be bytes or, if universal_newlines was True, a string. | |
communicate() returns a tuple (stdout_data, stderr_data). | |
Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too. | |
If the process does not terminate after timeout seconds, a TimeoutExpired exception will be raised. Catching this exception and retrying communication will not lose any output. | |
The child process is not killed if the timeout expires, so in order to cleanup properly a well-behaved application should kill the child process and finish communication: | |
""".encode('UTF-8')) | |
print(outs.decode('UTF-8')) |
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
from subprocess import Popen, PIPE | |
cmd = 'cat | head -5' | |
proc = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True) | |
outs, errs = proc.communicate(input=""" | |
Popen.communicate(input=None, timeout=None) | |
Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be data to be sent to the child process, or None, if no data should be sent to the child. The type of input must be bytes or, if universal_newlines was True, a string. | |
communicate() returns a tuple (stdout_data, stderr_data). | |
Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too. | |
If the process does not terminate after timeout seconds, a TimeoutExpired exception will be raised. Catching this exception and retrying communication will not lose any output. | |
The child process is not killed if the timeout expires, so in order to cleanup properly a well-behaved application should kill the child process and finish communication: | |
""") | |
print(outs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment