[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] [python]checke at et externt program findes



On Mon, 5 Jun 2006, Henrik Brix Andersen wrote:

On Mon, Jun 05, 2006 at 10:11:14AM +0200, Atte André Jensen wrote:
Jeg har et python script der bruger en del eksterne programmer (fx gv)
som jeg kalder via os.system, fx som "gv" altså uden sti. Nu vil jeg
gerne lave et check for om disse programmer er tilstede på systemet, men
hvordan gør jeg smartest det? Jeg ved jo ikke (og er ligeglad med)
hvilken sti de ligger i, så jeg kan ikke checke om filen findes og er
eksekverbar, hvilket var min første tanke.

Jeg ved ikke om Python har en speciel måde at håndtere dette, men jeg bruger normalt kommandoen 'command -v foobar' for at checke om 'foobar' findes i $PATH; fejler den, så findes 'foobar' ikke i $PATH.

En anden løsning holdt helt inden for python, vil være noget lignende følgende. search_path retunerer den fulde sti til et program, hvis den kan finde den; ellers returneres None.


Mvh, Jens

#! /usr/bin/env python
#
#
#

import os

def search_path(cmdname, path = None):
  if path is None:
    path = os.environ["PATH"]

  if os.name in ["nt", "os2"]:
    short = [cmdname + "." + ext for ext in ["exe","com","bat"]]
  else:
    short = [cmdname]

  for scmd in short:
    for dir in path.split(os.pathsep):
      fcmd = os.path.abspath(os.path.join(dir,scmd))
      if os.path.isfile(fcmd):
        return fcmd
  return None


if __name__ == "__main__": import sys if len(sys.argv) > 1: res = search_path(sys.argv[1]) if not res is None: print res else: print "Usage: %s command" % sys.argv[0]



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2006-07-01, 02:01 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *