RSS Forums RSS

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

Please support our Python advertiser: Programming Forums
Thread Solved
Reply
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

  #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"
AddThis Social Bookmark Button
Reply With Quote  
Posts: 770
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 139
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

  #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 2:32 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote  
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

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the Python Forum
Views: 359 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:43 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC