View Single Post
Join Date: Apr 2008
Posts: 3
Reputation: punmeister is an unknown quantity at this point 
Solved Threads: 0
punmeister punmeister is offline Offline
Newbie Poster

Help with finding out if a program exists in os path entries

 
0
  #1
Nov 26th, 2008
Hey hey!
I'm trying to write a script that finds out if a given program name exists in the OS environments PATH variable (cross plathform). Thing is, I get a NameError, it's probably a simple error, but I've struggling with it the last hour.

Any help would be appreciated
  1. #!/usr/bin/env python
  2. import os
  3. import os.path
  4.  
  5. """
  6. If the folder the program resides in is not added to the path the
  7. program will naturally not run, since the executing environment have
  8. no idea where it resides.
  9. """
  10.  
  11. if findprogram("mathlab")[0] == 1:
  12. print "W00t"
  13. else:
  14. print "Oh noes!"
  15.  
  16. def findprogram(program):
  17. path = os.environ['PATH']
  18. paths = path.split(os.pathsep)
  19. for d in paths:
  20. if os.path.isdir(d):
  21. fullpath = os.path.join(d, program)
  22. if sys.platform[:3] == 'win':
  23. for ext in '.exe', '.bat':
  24. if os.path.isfile(fullpath + ext):
  25. return 1, program_path
  26. else:
  27. if os.path.isfile(fullpath):
  28. return 1, program_path
  29. return 0, program + " command not found"
Reply With Quote