capturing output from os.system() in Windows?

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Apr 23 04:45:06 EDT 2003


gabnash at hotmail.com (David) wrote in 
news:f405d9e7.0304230003.63baab05 at posting.google.com:

> 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?

Us os.popen, os.popen2, os.popen3 or os.popen4 according to your exact 
needs.
e.g. lines = os.popen('net view').readlines()

N.B. Be careful if you try to separate stdout and stderr from the command 
you are running as you can get deadlocks if a pipe buffer fills up while 
you are trying to read from the other pipe.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list