LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-18-2008, 02:32 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
python: how to tell if an executable exists?


I want to see if a user has svn before trying to use it in a subprocess (this is for Linux machines only, if that matters).

Is there a quick way to tell if an executable exists somewhere in $PATH? I know I could run a subprocess of "which svn" and check the output, but I figure there's probably a quicker/more efficient way of doing it.

I would just use os.path.exists('/usr/bin/svn') but I can't guarantee that it will be there, so I'm looking for another method.
 
Old 06-18-2008, 04:42 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,387

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Code:
def which(exe):
    for dir in os.getenv("PATH").split(':'):                                           
        if (os.path.exists(os.path.join(dir, exe))):
            print 'found %s in %s' % (exe, dir)
Not sure how efficient that is (is efficiency really that important?), but this seems like the most straightforward way of doing it without using external programs.
 
Old 06-18-2008, 05:48 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
well, that certainly seems to do it. I was hoping for some magical built-in python thing, but it would probably end up doing the above, so... I'll take it.

Thanks!
 
Old 06-18-2008, 08:45 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can use the stat module and os.stat()
 
Old 06-18-2008, 09:29 PM   #5
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by ghostdog74 View Post
you can use the stat module and os.stat()
I'm not looking to see *if* the file is executable, rather, trying to find an executable file. I.e., is there a file on the system called "svn" somewhere in $PATH? I suppose seeing if its execute bit is set would be helpful too, but that's not what I'm after.

Thanks for the response, though.
 
Old 06-19-2008, 01:02 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
import os
for dir in os.getenv("PATH").split(':'): 
    for r,d,f in os.walk(dir):
        for files in f:
            if "svn" in files :
                print os.path.join(r,files)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Charming Python: Python elegance and warts, Part 2 LXer Syndicated Linux News 0 08-31-2007 09:40 AM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 08:54 PM
python executable deep_jyotsingh Programming 3 06-02-2006 05:09 AM
Running a Java executable class from another executable class LUB997 Programming 22 07-24-2005 05:57 AM
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 09:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration