10 examples of 'list all files in a directory python' in Python

Every line of 'list all files in a directory python' 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
this disclaimer
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
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

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
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
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
14def list_files(self, dircurrent):
15 for dirName, subdirList, fileList in os.walk(dircurrent, topdown=False):
16 for fname in fileList:
17 yield os.path.join(dirName, fname)
12def find_all_files(d):
13 return [
14 os.path.join(root, file)
15 for root, dirs, files in os.walk(d)
16 for file in files
17 ]
275def lsdirs(root=".", **kwargs):
276 """
277 Return only subdirectories from a directory listing.
278
279 Arguments:
280
281 root (str): Path to directory. Can be relative or absolute.
282 **kwargs: Any additional arguments to be passed to ls().
283
284 Returns:
285
286 list of str: A list of directory paths.
287
288 Raises:
289
290 OSError: If root directory does not exist.
291 """
292 paths = ls(root=root, **kwargs)
293 if isfile(root):
294 return []
295 return [_path for _path in paths if isdir(path(root, _path))]
9def _listdir(root):
10 ''' List directory 'root' appending the path separator to subdirs. '''
11 res = []
12 for name in os.listdir(root):
13 path = os.path.join(root, name)
14 if os.path.isdir(path):
15 name += os.sep
16 res.append(name)
17 return res
83def _find_files(dir_, regex='*.*'):
84 """Walk recursively through all dirs in 'dir_'.
85 Yield all files in 'dir_' matching 'regex' with absolute path
86 """
87 abs_path = os.path.abspath(dir_)
88 if not os.path.isdir(abs_path):
89 logging.warning('does not exist/is not a directory: ' + abs_path)
90 for root, dirnames, filenames in os.walk(abs_path):
91 for filename in fnmatch.filter(filenames, regex):
92 yield os.path.join(root, filename)
14def list_dir(dir_path, filter_func):
15 return sorted(filter(filter_func, os.listdir(dir_path)), key=get_name)

Related snippets