943,745 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 673
  • Python RSS
Nov 26th, 2008
0

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

Expand Post »
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
python Syntax (Toggle Plain Text)
  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"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
punmeister is offline Offline
3 posts
since Apr 2008
Nov 26th, 2008
0

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

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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Nov 27th, 2008
0

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

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
punmeister is offline Offline
3 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: passing int as 64 bit integer to COM
Next Thread in Python Forum Timeline: Text Copy in HtmlWindow





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC