Extracting path of a program from a list.

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Aug 27 06:57:37 CEST 2008


En Wed, 27 Aug 2008 01:15:58 -0300, aditya shukla  
<adityashukla1983 at gmail.com> escribi�:

> I wanna know how can i extract path of a program whose path i have added  
> to
> the PATH variable.
>
> This is what i have done
>
> import os
>
> x=os.getenv("PATH")
>
> print x
>
>>> %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;c:\Program
> Files\Microsoft SQL Server\90\Tools\binn\;C:\Program
> Files\QuickTime\QTSystem\;C:\folder1\folder2\prog
>
> Now i have to extract the path of my program ie  
> (C:\folder1\folder2\prog) .I
> mean i can split the string using
>
> y=x.split(';')
>
> a=y[-1]
>
> but i dont wanna do this way ,i  wanna search for my program from this  
> list
> and then return its path

Can't you get the program name and path from the place you got it  
originally, before adding it to the PATH variable?
Or store it in some other place. I think it's a lot safer that way.

Anyway, once you split the PATH, you get a list. You should iterate over  
the list looking if your program is located on each directory... Something  
like this:

path = os.getenv("PATH")
dirs = path.split(os.pathsep)
for dir in dirs:
   fullfn = os.path.join(dir, your_application_name_including_exe_extension)
   if os.path.isfile(fullfn):
     # found

-- 
Gabriel Genellina




More information about the Python-list mailing list