Alex Rivera | Logout

How can I get terminal output in python?

Asked 2010-12-10T11:31:31.250
49

I can execute a terminal command using os.system() but I want to capture the output of this command. How can I do this?

Edit
Report

1 Answer

35

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
answered 2010-12-10T11:33:15.693

Your Answer