does os.popen() search the path?

jschmitt at vmlabs.com jschmitt at vmlabs.com
Sun Oct 15 19:08:10 EDT 2000



In article <7PZF5.8592$wG1.27385 at news-server.bigpond.net.au>,
  "Mark Hammond" <MarkH at ActiveState.com> wrote:
> <jschmitt at vmlabs.com> wrote in message
> news:8s8ubv$21q$1 at nnrp1.deja.com...
>
> > I can get it to print the PATH environment variable and it tells me
> > that things like 'grep' are in my path.  Yet,
> >
> > cmd = os.popen( 'grep' )
> >
> > never launches.
>
> Never launches, or launches and quickly terminates?  "grep" with no
> options will attempt to read from stdin, but no stdin it setup, so it
> will exit immediately.

Never seems to launch.  Below is my script.  If I run this, I get the
same results as when I type 'dir' at the console.  If I change to 'dir'
to 'grep', nothing gets printed at all, not even the message that popen
failed.  When I type 'grep' on the console, I get a terse help message.

import sys
import os


#---------------------------------------------------------------------
if __name__ == '__main__':
    cmd = os.popen( 'dir' )
    if cmd:
        while 1:
            line = cmd.readline()
            if line:
                sys.stdout.write( line )
            else:
                break
    else:
        print 'popen failed'



>
> eg:
>
> >>> len(os.popen("grep --help").read())
> 2112
>
> > Another thing, I can also do this:
> >
> > cmd = os.popen( 'foo.cmd' )
>
> os.popen simply uses "cmd.exe" (or whatever your COMSPEC is, etc), so
> whatever behaviour you are seeing is cmd.exe at work.
>
> Have you tried:
>
> cmd = os.popen( os.path.abspath('foo.cmd') )
>
> ?

I actually gave os.popen() an absolute path.  But, never mind, because
I originally did this with a beta version, and tried it with Candidate1
and all is well on this front.  What I did was use the beta initially,
found problems, went to candidate1, and only tried the os.popen
( 'grep' ) again.  Sorry for the mix-up.


>
> Mark.
>
>

Thanks for the input.

John


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list