Re: Find file in a given search path

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gabriel Genellina

    #1

    Re: Find file in a given search path

    En Wed, 20 Aug 2008 17:10:15 -0300, aditya shukla <adityashukla19 83@gmail.comesc ribió:
    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(fil ename, search_path):
    """Given a search path, find file
    """
    for path in search_path.spl it(pathsep):
    fullpath = join(path, filename)
    if exists(fullpath ):
    return abspath(fullpat h)
    # there is an implicit return None at the end

    --
    Gabriel Genellina

Working...