10 examples of 'python list all files in directory and subdirectories with extension' in Python

Every line of 'python list all files in directory and subdirectories with extension' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
16def list_files(directory, extension):
17 file_list = []
18 for (dirpath, dirnames, filenames) in os.walk(directory + '/'):
19 for file in filenames:
20 if file.upper().endswith(extension):
21 file_list.append(os.path.join(dirpath,file))
22 return file_list
33def find_files(directory, extensions):
34 '''recursively look for files that end in the extensions tuple (case
35 insensitive)'''
36 matches = []
37 for root, dirnames, filenames in os.walk(directory):
38 for d in dirnames[:]:
39 if d.startswith('.') or d == 'RECYCLE' or d == '$Recycle':
40 dirnames.remove(d)
41 for filename in filenames:
42 for ext in extensions:
43 if re.search(r'\.' + ext + '$', filename, re.I):
44 matches.append(os.path.join(root, filename))
45 break
46 return matches
105def listdir(dir, base, ext):
106 ret = []
107 for (d, _, files) in os.walk(dir):
108 for f in files:
109 if not ext or os.path.splitext(f)[1].lower() in ext:
110 if not base or base.lower() in f.lower():
111 ret.append(os.path.normpath(os.path.join(d, f)))
112 return ret
22def list_files(directories, filters):
23 matches = []
24 for directory in directories:
25 for root, _, filenames, in os.walk(os.path.join('codehero', directory)):
26 for f in filters:
27 for filename in fnmatch.filter(filenames, f):
28 matches.append(os.path.join(root, filename))
29 return matches
17def list_files_in_dir( folder, extension ):
18 output = glob.glob( folder + '/*' + extension )
19
20 index = 1
21 while True:
22 ending_pf = "_" + str( index ) + extension
23 initial_size = len( output )
24 output = [v for v in output if not v.endswith( ending_pf )]
25 if initial_size == len( output ):
26 break;
27 index = index + 1
28
29 return output
22def list_files(folder,ext_filter=None):
23 onlyfiles = []
24 for f in os.listdir(folder):
25 fullpath = os.path.join(folder, f)
26 if os.path.isfile(fullpath) and os.path.splitext(fullpath)[1]==ext_filter:
27 onlyfiles.append(fullpath)
28 return onlyfiles
4def find_files(basepath, extension=''):
5
6 found_files = []
7
8 for subdir, dirs, files in os.walk(basepath):
9 for file in files:
10 if file.endswith(extension):
11 file_path = os.path.join(subdir, file)
12 found_files.append(file_path)
13 return found_files
301def list_all_files(dir):
302 try:
303 filenames = [name for name in os.listdir(dir) if os.path.isfile(os.path.join(dir, name))]
304 return filenames
305 except Exception as e:
306 if DEBUG_FLAG:
307 sys.stderr.write("Naked Framework Error: unable to generate directory file list with the list_all_files() function (Naked.toolshed.system).")
308 raise e
49def list_files(path):
50 if not exists(path, folder=True):
51 return []
52 f = []
53 for root, dirs, files in os.walk(path):
54 f.extend([join(root, _file) for _file in files])
55 return f
28def list_files(path):
29 """Recursively collects a list of files at a path."""
30 files = []
31 if os.path.isdir(path):
32 for stats in os.walk(path):
33 for f in stats[2]:
34 files.append(os.path.join(stats[0], f))
35 elif os.path.isfile(path):
36 files = [path]
37 return files

Related snippets