Execute CLI commands in Python

· Thomas Taylor

Executing CLI commands / shell scripts / external programs in Python is easy.


Since Python 3.5 ( for earlier versions), is the recommended way to execute external shell scripts.

1import subprocess
3output =["echo", "hello", "world"], capture_output=True)
4print(output.returncode) # outputs 0
5print(output.stdout.decode("utf-8")) # outputs hello world


Reply to this post by email ↪