Can I capture console output from an os.system() call?

Alex Martelli aleaxit at
Sat Mar 3 16:49:23 CET 2001

"Brian Geddes" <brian.j.geddes at> wrote in message
news:97p7nt$a2s at
> All -
> When I call a foreign program using an os.system("program.exe") call, is
> possible to get at the console output of "program.exe" from my python
> script?

If it's actually *console* output (performed through the Windows API
for doing console output), no.  If it is, as in the following example,
output performed to standard-handles, then, yes, but you will have
to use os.popen rather than os.system.

> If "program.exe" spits out a message "Hello World!" to the screen when run
> from the DOS command line, and I call 'os.system("program.exe")' as a line
> of my Python script, how do I get at the "Hello World!" message?

If program.exe chooses to perform its output directly 'to the screen'
(via the Console API's of Windows, or other means, such as poking
into the simulated screen memory etc), you're out of luck.  If it more
normally chooses to write to 'standard output', then run it via
xx=os.popen("program.exe","r") -- the returned xx object is a file
like object from which you can read from.


More information about the Python-list mailing list