How to get output of command called by os.system()?

Rominsky john.rominsky at gmail.com
Sat Oct 31 05:11:48 EDT 2009


On Oct 30, 11:09 pm, Peng Yu <pengyu... at gmail.com> wrote:
> I need to integrate shell program with python. I'm wondering if there
> is a way get the output of the shell program called by os.system().
> Thank you!

popen should do what your after.  There are several modules that have
a popen method including os and subprocess.  It will allow you to make
a system call similar to os.system, but it gives you pipe access, like
an open file, to the standard output and standard error if you use
subprocess.Popen.  A simple example would be:

import subprocess
output = subprocess.Popen('pwd')
print('Present Working Directory is: ' + output.readline())

Hope that helps.

-John



More information about the Python-list mailing list