how.wtf

Execute CLI commands in Python

· Thomas Taylor

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

Use subprocess.run

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

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

#Python  

Reply to this post by email ↪