output of shell command

Ben Hutchings ben.hutchings at roundpoint.com
Tue Feb 27 17:01:11 EST 2001


"Mark Hadfield" <m.hadfield at niwa.cri.nz> writes:

> "Steve Holden" <sholden at holdenweb.com> wrote in message
> news:UYAm6.21417$P87.223264 at e420r-atl2.usenetserver.com...
> > Note, however, that under PythonWin:
> >
> > >>> import os
> > >>> a=os.popen("dir").readlines()
> > Traceback (innermost last):
> >   File "<interactive input>", line 1, in ?
> > WindowsError: [Errno 2] The system cannot find the file specified
> 
> I found, as you did that, this works only in console Python (using Python
> 2.0 + win32all-137 on Windows 2000). However the following
> 
> a=(os.popen("dir")).readlines()
> 
> works (returns a string array) in both console Python and PythonWin .
> 
> > Not sure why it works with a vanilla interpreter but not in PythonWin.
> 
> A subtle difference in operator precedence?

Oh, come on.  One thing that absolutely mustn't vary between different
Python environments is operator precedence.

This may have something to do with the fact that the DOS/Windows "dir"
command is built into the standard command interpreter (command.com or
cmd.exe) and is not callable directly - CreateProcess("dir", ...)
won't work but CreateProcess("command", "command /c dir", ...) will.

os.popen("dir").readlines() works for me in PythonWin 2.0 #8 on
Windows 2000 SP1, but I would not be surprised to find that it fails
to work on Windows 98, though - there are large differences between
the command interpreters and pipe implementations in different
versions of Windows.  For that matter, the output format of dir may
differ too.

-- 
Any opinions expressed are my own and not necessarily those of Roundpoint.



More information about the Python-list mailing list