Reputation: 1773
Given a list of file names, I want to create the full path for these files if they exist in a given directory or it's sub-directories. Right now I use this code
def construct_path(file_list, directory):
file_path_list = []
for name in file_list:
for dir, subdir, filenames in os.walk(directory):
if name in filenames:
file_path_list.append(os.path.join(dir, name))
return file_path_list
So here the directory is being crawled for each file in the list. Is there a faster/better way to do it?
Upvotes: 0
Views: 2464
Reputation: 21766
You can remove the loop over the file_list
and remove found files from this list, so you can return the results once you have found all your files:
def construct_path(file_list, directory):
file_path_list = []
for dir, subdir, files in os.walk(directory):
for name in files:
if name in file_list:
file_path_list.append(os.path.join(dir, name))
file_list.remove(name)
if (len(file_list)==0): return file_path_list
return file_path_list
Upvotes: 2