| | |
Help with finding out if a program exists in os path entries
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 3
Reputation:
Solved Threads: 0
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
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)
#!/usr/bin/env python import os import os.path """ If the folder the program resides in is not added to the path the program will naturally not run, since the executing environment have no idea where it resides. """ if findprogram("mathlab")[0] == 1: print "W00t" else: print "Oh noes!" def findprogram(program): path = os.environ['PATH'] paths = path.split(os.pathsep) for d in paths: if os.path.isdir(d): fullpath = os.path.join(d, program) if sys.platform[:3] == 'win': for ext in '.exe', '.bat': if os.path.isfile(fullpath + ext): return 1, program_path else: if os.path.isfile(fullpath): return 1, program_path return 0, program + " command not found"
You're calling the function findprogram before defining it.... if you simply rearrange your code so that the
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:
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)
#!/usr/bin/python import #whatever needs imported def # Define some functions def main(): # Do anything that needs done here # Call functions, etc... if __name__ == '__main__': main()
Last edited by jlm699; Nov 26th, 2008 at 3:32 pm.
![]() |
Other Threads in the Python Forum
- Previous Thread: passing int as 64 bit integer to COM
- Next Thread: Text Copy in HtmlWindow
Views: 452 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Python
anti assignment avogadro beginner bluetooth code convert count csv decimals def dictionary dynamic dynamically enter examples excel file float format frange ftp function gnu gui heads homework import input jaunty java lapse leftmouse line lines linux list lists loop microcontroller module mouse multiple newb number numbers output parsing path pointer port prime program programming projects py2exe pygame pygtk pyopengl pyqt python random raw_input recursion recursive redirect scrolledtext slicenotation software sqlite ssh stderr string strings subprocess syntax table tennis terminal text thread threading time tkinter tlapse tooltip tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape windows word wx.wizard wxpython






