Find file in a given search path
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Wed Aug 20 17:11:51 EDT 2008
En Wed, 20 Aug 2008 17:10:15 -0300, aditya shukla <adityashukla1983 at gmail.com> escribió:
> Hello folks, i am trying to find a particular file in a given search path.I
> found code for this on internet.
>
> if __name__ == '___main__':
> search_path = '/bin' + pathsep + '/usr/bin' # ; on windows, : on unix
> find_file = search_file('ls',search_path)
> if find_file:
> print "File found at %s" % find_file
> else:
> print "File not found"
>
>
> Whenever i try to run the code ie python findpath.py , nothing happens , i
> mean command prompt reappears .
You have 3 leading underscores in __main__ so the if statement is skipped and nothing happens.
I'd use the split method of string objects, and there is no need for a file_found variable:
def search_file(filename, search_path):
"""Given a search path, find file
"""
for path in search_path.split(pathsep):
fullpath = join(path, filename)
if exists(fullpath):
return abspath(fullpath)
# there is an implicit return None at the end
--
Gabriel Genellina
More information about the Python-list
mailing list