Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
portable way of locating an executable (like which)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  15 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Gelonida N  
View profile  
 More options Sep 20 2012, 5:07 pm
Newsgroups: comp.lang.python
From: Gelonida N <gelon...@gmail.com>
Date: Thu, 20 Sep 2012 23:06:46 +0200
Local: Thurs, Sep 20 2012 5:06 pm
Subject: portable way of locating an executable (like which)
I'd like to implement the equivalent functionality of the unix command
/usr/bin/which

The function should work under Linux and under windows.

Did anybody already implement such a function.
If not, is there a portable way of splitting the environment variable PATH?

Thanks for any sugestions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Lawrence  
View profile  
 More options Sep 20 2012, 5:47 pm
Newsgroups: comp.lang.python
From: Mark Lawrence <breamore...@yahoo.co.uk>
Date: Thu, 20 Sep 2012 22:47:11 +0100
Local: Thurs, Sep 20 2012 5:47 pm
Subject: Re: portable way of locating an executable (like which)
On 20/09/2012 22:06, Gelonida N wrote:

> I'd like to implement the equivalent functionality of the unix command
> /usr/bin/which

> The function should work under Linux and under windows.

> Did anybody already implement such a function.

Searching found nothing obvious to me :(

> If not, is there a portable way of splitting the environment variable PATH?

With os.sep ?

> Thanks for any sugestions

--
Cheers.

Mark Lawrence.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Sep 20 2012, 6:21 pm
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Fri, 21 Sep 2012 08:21:27 +1000
Local: Thurs, Sep 20 2012 6:21 pm
Subject: Re: portable way of locating an executable (like which)

On Fri, Sep 21, 2012 at 7:47 AM, Mark Lawrence <breamore...@yahoo.co.uk> wrote:
> On 20/09/2012 22:06, Gelonida N wrote:

>> I'd like to implement the equivalent functionality of the unix command
>> /usr/bin/which

>> The function should work under Linux and under windows.

>> Did anybody already implement such a function.

> Searching found nothing obvious to me :(

>> If not, is there a portable way of splitting the environment variable
>> PATH?
> With os.sep ?

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.

Alternatively, there may be a Win32 API funct5ion that does this.
Would be worth a look.

ChrisA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Sep 20 2012, 6:33 pm
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Thu, 20 Sep 2012 16:32:42 -0600
Local: Thurs, Sep 20 2012 6:32 pm
Subject: Re: portable way of locating an executable (like which)

On Thu, Sep 20, 2012 at 4:21 PM, Chris Angelico <ros...@gmail.com> 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]


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Sep 20 2012, 7:04 pm
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Fri, 21 Sep 2012 09:04:06 +1000
Local: Thurs, Sep 20 2012 7:04 pm
Subject: Re: portable way of locating an executable (like which)

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gelonida N  
View profile  
 More options Sep 20 2012, 7:08 pm
Newsgroups: comp.lang.python
From: Gelonida N <gelon...@gmail.com>
Date: Fri, 21 Sep 2012 01:07:08 +0200
Local: Thurs, Sep 20 2012 7:07 pm
Subject: Re: portable way of locating an executable (like which)
On 09/21/2012 12:21 AM, Chris Angelico wrote:

> On Fri, Sep 21, 2012 at 7:47 AM, Mark Lawrence <breamore...@yahoo.co.uk> wrote:
>> On 20/09/2012 22:06, Gelonida N wrote:

>>> I'd like to implement the equivalent functionality of the unix command
>>> /usr/bin/which

>>> The function should work under Linux and under windows.

>>> Did anybody already implement such a function.

>> Searching found nothing obvious to me :(

I was afraid so, but wanted to be sure

>>> If not, is there a portable way of splitting the environment variable
>>> PATH?
>> With os.sep ?

> os.sep is the directory separator, but os.pathsep may be what you
> want.

Thanks,
os.pathsep was the missing piece for portably splitting the searchpath

>  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.

For what I am doing I can even skip trying the pathexts, the ext is
already given, but good to know :-)

> Alternatively, there may be a Win32 API funct5ion that does this.
> Would be worth a look.

Yeah true, but ideally I'd like to avoid platform detection and
just have a generic function.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gelonida N  
View profile  
 More options Sep 20 2012, 7:15 pm
Newsgroups: comp.lang.python
From: Gelonida N <gelon...@gmail.com>
Date: Fri, 21 Sep 2012 01:15:02 +0200
Local: Thurs, Sep 20 2012 7:15 pm
Subject: Re: portable way of locating an executable (like which)
On 09/21/2012 12:04 AM, Jason Swails wrote:

Thanks a lot Jason,

I'll try it, the script looks reasonably portable (using os.pathsep)
to really replicate which I had probably to add os.getenv('pathext')
as Chris mentioned.
However for my current use case this is not necessarily required.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Lawrence  
View profile  
 More options Sep 20 2012, 7:46 pm
Newsgroups: comp.lang.python
From: Mark Lawrence <breamore...@yahoo.co.uk>
Date: Fri, 21 Sep 2012 00:46:48 +0100
Local: Thurs, Sep 20 2012 7:46 pm
Subject: Re: portable way of locating an executable (like which)
On 21/09/2012 00:15, Gelonida N wrote:

http://nedbatchelder.com/code/utilities/wh_py.html

--
Cheers.

Mark Lawrence.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nobody  
View profile  
 More options Sep 20 2012, 7:59 pm
Newsgroups: comp.lang.python
From: Nobody <nob...@nowhere.com>
Date: Fri, 21 Sep 2012 00:59:53 +0100
Local: Thurs, Sep 20 2012 7:59 pm
Subject: Re: portable way of locating an executable (like which)

On Thu, 20 Sep 2012 23:06:46 +0200, Gelonida N wrote:
> I'd like to implement the equivalent functionality of the unix command
> /usr/bin/which

> The function should work under Linux and under windows.

Note that "which" attempts to emulate the behaviour of execvp() etc. The
exec(3) manpage will explain the precise algorithm used (e.g. they skip
files for which the process lacks execute permission).

Also, note that the shell has built-in commands, functions, and aliases in
addition to programs. The "type" built-in command performs a similar
function to "which" but using the shell's semantics. On some systems,
the default configuration may alias "which" to "type".

On Windows, there's a host of different "execute program" interface, all
with subtly different semantics: which extensions they will run, which
extensions can be omitted, which paths are used (e.g. %PATH%, paths
from the registry, current directory).


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Angel  
View profile  
 More options Sep 20 2012, 10:31 pm
Newsgroups: comp.lang.python
From: Dave Angel <d...@davea.name>
Date: Thu, 20 Sep 2012 22:31:17 -0400
Local: Thurs, Sep 20 2012 10:31 pm
Subject: Re: portable way of locating an executable (like which)
On 09/20/2012 06:04 PM, Jason Swails wrote:

I don't have a Windows machine set up right now, but I believe there are
two more directories to search, besides the ones described in the PATH
variable.

One is the current directory, and the other is the Windows directory
(maybe also the xxx/system32 or something).

They don't have analogues in Linux or Mac, as far as I know.

--

DaveA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Berg  
View profile  
 More options Sep 20 2012, 10:57 pm
Newsgroups: comp.lang.python
From: Andrew Berg <bahamutzero8...@gmail.com>
Date: Thu, 20 Sep 2012 21:57:14 -0500
Local: Thurs, Sep 20 2012 10:57 pm
Subject: Re: portable way of locating an executable (like which)
On 2012.09.20 21:31, Dave Angel wrote:
> I don't have a Windows machine set up right now, but I believe there are
> two more directories to search, besides the ones described in the PATH
> variable.

> One is the current directory, and the other is the Windows directory
> (maybe also the xxx/system32 or something).

Those system directories are in the path by default.

--
CPython 3.3.0rc2 | Windows NT 6.1.7601.17835


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tarek Ziadé  
View profile  
 More options Sep 21 2012, 4:18 am
Newsgroups: comp.lang.python
From: Tarek Ziadé <ta...@ziade.org>
Date: Fri, 21 Sep 2012 10:12:29 +0200
Local: Fri, Sep 21 2012 4:12 am
Subject: Re: portable way of locating an executable (like which)
On 9/21/12 1:59 AM, Nobody wrote:

You can also look at shutil.which

http://hg.python.org/cpython/file/aa153b827d17/Lib/shutil.py#l974

Mmmm I wonder why it's removed in the last revs..


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hans Mulder  
View profile  
 More options Sep 21 2012, 6:14 am
Newsgroups: comp.lang.python
From: Hans Mulder <han...@xs4all.nl>
Date: Fri, 21 Sep 2012 12:13:48 +0200
Local: Fri, Sep 21 2012 6:13 am
Subject: Re: portable way of locating an executable (like which)
On 21/09/12 04:31:17, Dave Angel wrote:

On Posix systems, you need to insert at this point:

            if not path:
                path = "."

On Posix system (inlcuding Linux and Mac OS X), the current
directory is not searched by default.  If there's an empty
string in os.getenv("PATH").split(os.pathsep), then the
current directory will be searched at that point in the part.

Hope this helps,

-- HansM


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ramchandra Apte  
View profile  
 More options Sep 22 2012, 11:55 pm
Newsgroups: comp.lang.python
From: Ramchandra Apte <maniandra...@gmail.com>
Date: Sat, 22 Sep 2012 20:55:46 -0700 (PDT)
Local: Sat, Sep 22 2012 11:55 pm
Subject: Re: portable way of locating an executable (like which)

shutil.which does this in Python 3.3: http://docs.python.org/dev/library/shutil.html#shutil.which
You can copy the code to support older Python versions.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ramchandra Apte  
View profile  
 More options Sep 22 2012, 11:55 pm
Newsgroups: comp.lang.python
From: Ramchandra Apte <maniandra...@gmail.com>
Date: Sat, 22 Sep 2012 20:55:46 -0700 (PDT)
Local: Sat, Sep 22 2012 11:55 pm
Subject: Re: portable way of locating an executable (like which)

shutil.which does this in Python 3.3: http://docs.python.org/dev/library/shutil.html#shutil.which
You can copy the code to support older Python versions.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »