471,595 Members | 1,040 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

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
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 1201

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
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.