Win XP: Problem with shell scripting in Python

John Machin sjmachin at lexicon.net
Fri Jun 9 02:47:37 CEST 2006


On 9/06/2006 8:58 AM, A.M wrote:
> Hi,
> I am having difficulty with shell scripting in Python.
> I use the following command to run a DOS command and put the return value in 
> a Python variable:
> print os.popen('DIR').read()
> It works very fine with DIR command, but for commands like "MD :" it doesn't 
> return the error message into the string:
> print os.popen('MD :').read()
> # No error message
> When I use Ruby, it works perfect:
> `md :`
> 

Irrelevant; different "it".

> The filename, directory name, or volume label syntax is incorrect.
> 
> 

I have never had occasion to use os.popen_anything before. Perhaps we 
can aid each other on the path to enlightenment. I got the following 
idea from looking at the manual.

|>>> handles = os.popen3('MD :')
|>>> [f.read() for f in handles[1:]]
['', 'The filename, directory name, or volume label syntax is incorrect.\n']
|>>> [f.close() for f in handles]
[None, None, 1]

|>>> handles = os.popen3('dir *.py')
|>>> [f.read() for f in handles[1:]]
[' Volume in drive C has no label.\n Volume Serial Number is **BIG 
SNIP** bytes free\n', '']
|>>> [f.close() for f in handles]
[None, None, None]

Windows does occasionally adhere to *x conventions like "data to stdout, 
error messages to stderr" :-)

Now it's *your* turn to do something for the cause. It appears to me 
that popen4 has exactly the same documentation as popen3, as recently as 
2.5a2. I see no fourth gizmoid here.

Whoooaaah! "4" is not a gizmoid count:

|>>> handles = os.popen4('MD :')
|>>> handles
(<open file 'MD :', mode 'w' at 0x00AAF698>, <open file 'MD :', mode 'r' 
at 0x00
AAF4E8>)

You might like to suss this out, and raise a request to have the docs fixed.

Cheers,
John



More information about the Python-list mailing list