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 –
1 2 3 4 5 6 |
import subprocess p = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE).stdout.decode('utf-8') print(p) ## Returns the output of ls -l shell command |