capturing output from os.system() in Windows?

Gary Herron gherron at islandtraining.com
Wed Apr 23 04:32:28 EDT 2003


On Wednesday 23 April 2003 01:03 am, David wrote:
> Being a complete novice at python, I thought I'd try a simple script
> to run a NET VIEW (which runs in a DOS box) command under Windows and
> then manipulate the resulting data.
>
> The first problem I have encountered is how to capture the output from
> the 'NET VIEW' command.
>
> os.system('NET VIEW') returns 0 under windows, although the DOS box
> pops up and the NET VIEW executes correctly.
>
> Is there some other command I should be using instead? I guess an
> alternative might be to run the 'NET VIEW' under dos and redirect the
> output into a python script using a BAT file?
>
> Any ideas?

Redirect works, but here's a better idea.

Use one of the functions in the popen2 module. These functions execute
a command with output going to a pipe (or several pipes) which can be
read by your program.

Gary Herron









More information about the Python-list mailing list