By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,818 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,818 IT Pros & Developers. It's quick & easy.

Re: Find file in a given search path

P: n/a
En Wed, 20 Aug 2008 17:10:15 -0300, aditya shukla <ad**************@gmail.comescribió:
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

Aug 20 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.