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

Thread Solved
Reply

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 Quick reply to this message  
Join Date: Jul 2008
Posts: 1,009
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 244
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

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

 
0
  #2
Nov 26th, 2008
You're calling the function findprogram before defining it.... if you simply rearrange your code so that the def findprogram(program): block is first, you'll solve the problem. You'll then find that you forgot to import sys
And then you'll find that you're trying to use some variable called program_path when instead you might have wanted fullpath

A good way to avoid this is to not ever let your code hang out in the ether like that... commonplace is to place your code all into functions, and have a main() function to call them... Here's a pretty commonly used method:
  1. #!/usr/bin/python
  2. import #whatever needs imported
  3.  
  4. def # Define some functions
  5.  
  6. def main():
  7. # Do anything that needs done here
  8. # Call functions, etc...
  9.  
  10. if __name__ == '__main__':
  11. main()
Last edited by jlm699; Nov 26th, 2008 at 3:32 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
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

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

 
0
  #3
Nov 27th, 2008
Crap, I see that now. I can't belive I'd forgotton how C-like python was. Is there any way you can define prototypes in python like C?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC