On Thu, 26 May 2005 11:53:04 -0700, Don <[EMAIL PROTECTED]> wrote: >Steven Bethard wrote: > >> This has probably been answered before, but my Google skills have failed >> me so far... >> >> Is there an os independent way of checking to see if a particular >> executable is on the path? Basically what I want to do is run code like: >> i, o, e = os.popen3(executable_name) >> but I'd like to give an informative error if 'executable_name' doesn't >> refer to an executable on the path. >> >> [snip] >> >> Thanks for the help, >> >> STeVe > >I wrote this 'which' function for Linux, but I think if you changed the ':' >character, it would work on Windows (I think its a ';' on Windows, but I >can't remember): > >def which( command ): > path = os.getenv( 'PATH' ).split( ':' ) > found_path = '' > for p in path: > try: > files = os.listdir( p ) > except: > continue > else: > if command in files: > found_path = p > break > > return found_path >
Here's the version that comes with Twisted: import os, sys, imp def which(name, flags=os.X_OK): """Search PATH for executable files with the given name. @type name: C{str} @param name: The name for which to search. @type flags: C{int} @param flags: Arguments to L{os.access}. @rtype: C{list} @param: A list of the full paths to files found, in the order in which they were found. """ result = [] exts = filter(None, os.environ.get('PATHEXT', '').split(os.pathsep)) for p in os.environ['PATH'].split(os.pathsep): p = os.path.join(p, name) if os.access(p, flags): result.append(p) for e in exts: pext = p + e if os.access(pext, flags): result.append(pext) return result Jp -- http://mail.python.org/mailman/listinfo/python-list