faqts : Computers : Programming : Languages : Python : Modules : os

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

107 of 117 people (91%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How can I run another application (non Python) from within a Python script and capture its output?
Does os.popen() work from pythonw?

Jun 4th, 2003 03:56
Xcrash, Nathan Wallace, Jesper Hertel, Niels Diepeveen


os.popen(command_line) returns a pipe from wich you can read the
standard output from the command given by command_line.

That is, for example,

     a = os.popen("dir").read()

runs the command "dir" and puts the entire output from the command in
the variable a.

Another function os.popen2() also returns standard error output from the
command, but this does not work in Windows, unfortunately.

Be careful, os.popen only works in a console session, not from pythonw.
This is a problem in the C library.

Also a handy function from the commands module would suffice..

import commands

x = commands.getoutput('ls')