Last active
July 10, 2017 14:14
-
-
Save pddg/37b99cba7f8a6cd8b6bb2c70fa9a8484 to your computer and use it in GitHub Desktop.
Subprocess stdout
This file contains 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
$ python -V | |
3.6.0 | |
$ python sub_stdout.py | |
Execute: echo 'test' | |
Result: test | |
<class 'str'> |
This file contains 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
import subprocess | |
def command(cmd): | |
print("Execute: " + cmd) | |
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
stdout_data, stderr_data = p.communicate() | |
str_stdout = stdout_data.decode() | |
print("Result: " + str_stdout) | |
print(type(str_stdout)) | |
if __name__ == "__main__": | |
command("echo 'test'") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment