miltonjbradley miltonjbradley - 1 year ago 125
Bash Question

python os.system command line return value back to python

I am calling a second python script that is written for the command line from within my script using

os.system('insert command line arguments here')

this works fine and runs the second script in the terminal. I would like this not to be output in the terminal and simply have access to the lists and variables that are being printed. Is this possible using os.system? Or, do I need to use something else?


You can use the subprocess module and redirect its output through a pipe.

For example, to get the list of file in current directory.

import subprocess
proc = subprocess.Popen(['ls'], stdout=subprocess.PIPE)

More details here Redirecting stdio from a command in os.system() in python