Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: portable way of locating an executable (like which)

Reply
Thread Tools

Re: portable way of locating an executable (like which)

 
 
Chris Angelico
Guest
Posts: n/a
 
      09-20-2012
On Fri, Sep 21, 2012 at 8:32 AM, Ian Kelly <(E-Mail Removed)> wrote:
> On Thu, Sep 20, 2012 at 4:21 PM, Chris Angelico <(E-Mail Removed)> wrote:
>> os.sep is the directory separator, but os.pathsep may be what you
>> want. Between that and os.getenv('path') you can at least get the
>> directories. Then on Windows, you also need to check out
>> os.getenv('pathext') and split _that_ on the semicolon, and try each
>> of those as a file extension. I'm not sure whether or not Windows will
>> add extensions from pathext if one is given on the command line - for
>> instance, if typing "foo.exe" will search for "foo.exe.bat" - but the
>> basics are there.

>
> Easy enough to test:
>
> C:\>echo echo hello! > foo.exe.bat
>
> C:\>foo.exe
> hello!
>
> Yup, it does. It looks like it tries it without the extension first, though:
>
> C:\>copy c:\windows\notepad.exe foo.exe
> 1 file(s) copied.
>
> C:\>foo.exe
> [starts notepad]


Well, at least it's consistent. Makes your PATH extremely sensitive,
though, easy for anyone to inject executables into it. But then, you
can already do that by putting them in the current directory, so
that's not really any different.

Jason's solution looks fine apart from the PATHEXT requirement, so if
you know you have the full filename and you don't care if the actual
command interpreter will do exactly the same, that'll do you fine.

Is this something that might want to be a function in the os module?
Particularly so if, as I suspect there might be, there's a Win32 API
function that precisely replicates the behaviour of executable
invocation. A while since I've done much Windows programming but I
think there's a SearchPath function?

ChrisA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
portable way of locating an executable (like which) Gelonida N Python 4 09-23-2012 03:55 AM
Re: portable way of locating an executable (like which) Gelonida N Python 0 09-20-2012 11:07 PM
Re: portable way of locating an executable (like which) Ian Kelly Python 0 09-20-2012 10:32 PM
Re: portable way of locating an executable (like which) Chris Angelico Python 0 09-20-2012 10:21 PM
Re: portable way of locating an executable (like which) Mark Lawrence Python 0 09-20-2012 09:47 PM



Advertisments