You can use Popen in subprocess
as they suggest.
with os
, which is not recomment, it's like below:
import os
a = os.popen('pwd').readlines()
The easiest way is to use the library commands
import commands
print commands.getstatusoutput('echo "test" | wc')
The recommended way in Python 3.5 and above is to use subprocess.run()
:
from subprocess import run
output = run("pwd", capture_output=True).stdout
Source: Stackoverflow.com