469,271 Members | 1,032 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Re: Find file in a given search path

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
0 1123

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by hokieghal99 | last post: by
6 posts views Thread by Peter Hansen | last post: by
12 posts views Thread by Xah Lee | last post: by
reply views Thread by John Doe | last post: by
3 posts views Thread by Soulless | last post: by
8 posts views Thread by inFocus | last post: by
7 posts views Thread by latalui | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.