Using popen in windows

Gordon McMillan gmcm at hypernet.com
Thu Apr 20 11:12:45 EDT 2000


Robert Cragie

> I tried this and it still hangs; cvs.exe is definitely not a built in:
> 
> from win32pipe import popen
> p = popen('c:\\cygwin\\contrib\\bin\\cvs -v','r')
> str = p.read()
> print str
> 
> Again, the equivalent works fine on Linux. 

Works fine on NT, too. Bill Tutt put out something that works 
around the Win9x bug. Searching dejanews on popen and 
Win95 should find it.

> So how do I run a command in
> Windows and capture the output? The following works, but is clumsy, and you
> get the annoying DOS box while it runs - is there a better way?:
> 
> import os
> os.system('dir >temp')
> f = open('temp', 'r')
> f.read()

Yeah, os.listdir('.') <wink>.

To get around the singing, dancing consoles, you need to use  
os.spawnv, which is another whole can of worms.

Python 1.5.42+ (#0, Mar 28 2000, 20:39:47) [MSC 32 bit 
(Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, 
Amsterdam
>>> import os
>>> os.spawnv(os.P_WAIT, 'd:\\util\\bin\\cvs.exe', 
('d:\\util\\bin\\cvs.exe', '-v'))

Concurrent Versions System (CVS) 1.9 (client)

Copyright (c) 1993-1994 Brian Berliner
Copyright (c) 1993-1994 david d `zoo' zuhn
Copyright (c) 1992, Brian Berliner and Jeff Polk
Copyright (c) 1989-1992, Brian Berliner

CVS may be copied only under the terms of the GNU General 
Public License,
a copy of which can be found with the CVS distribution kit.
0
>>>

If you really want to understand this crap, look at 
win32process.CreateProcess(), because every method of 
method of starting a new process on Windows ends up 
making this call; and the cruntime lib (*nix emulation) calls are 
all hopelessly braindead in some way or other.

- Gordon




More information about the Python-list mailing list