Running shell command and capturing the output in Python

You can use subprocess.run() method of subprocess module to execute and capture the output of a shell command inside a Python script as shown in the below example –