Skip to content

Instantly share code, notes, and snippets.

@pddg
Last active July 10, 2017 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pddg/37b99cba7f8a6cd8b6bb2c70fa9a8484 to your computer and use it in GitHub Desktop.
Save pddg/37b99cba7f8a6cd8b6bb2c70fa9a8484 to your computer and use it in GitHub Desktop.
Subprocess stdout
$ python -V
3.6.0
$ python sub_stdout.py
Execute: echo 'test'
Result: test
<class 'str'>
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