def get_files(directory, pattern, recursive=True):
""" Return the full path to all files in directory matching the specified
pattern.
Arguments:
directory (str): Directory path in which to look
pattern (str): A glob pattern for filenames
recursive (bool): Searches recursively if True
Returns:
A list of matching file paths
"""
# This yields an iterator which really speeds up looking through large, flat directories
if recursive is False:
it = glob.iglob(os.path.join(directory, pattern))
return it
# If we want to recurse, use os.walk instead
matches = list()
for root, dirnames, filenames in os.walk(directory):
matches.extend([os.path.join(root, ss) for ss in
fnmatch.filter(filenames, pattern)])
return matches
评论列表
文章目录