10 examples of 'loop through all files in a directory python' in Python

Every line of 'loop through 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
9def process_dir(base_path, process_func):
10 for fp in iter_files(base_path):
11 process_func(fp)
30def walk_files(directory, match='*'):
31 """Generator function to iterate through all files in a directory
32 recursively which match the given filename match parameter.
33 """
34 for root, dirs, files in os.walk(directory):
35 for filename in fnmatch.filter(files, match):
36 yield os.path.join(root, filename)
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)
221def recurse_find_python_files(folder_io, except_paths=()):
222 for folder_io, file_io in recurse_find_python_folders_and_files(folder_io, except_paths):
223 if file_io is not None:
224 yield file_io
380def search(search_paths):
381 if not isinstance(search_paths, (list, tuple)):
382 search_paths = [search_paths]
384 for search_path in search_paths:
385 LOG.debug("Recursively collecting YAMLs from %s", search_path)
386 for root, _, filenames in os.walk(search_path):
388 # Ignore hidden folders like .tox or .git for faster processing.
389 if os.path.basename(root).startswith("."):
390 continue
391 # Skip over anything in tools/ because it will never contain valid
392 # Pegleg-owned manifest documents.
393 if "tools" in root.split("/"):
394 continue
396 for filename in filenames:
397 # Ignore files like .zuul.yaml.
398 if filename.startswith("."):
399 continue
400 if filename.endswith(".yaml"):
401 yield os.path.join(root, filename)
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 ]
247def run_files_from_dir(directory, script_args=None):
248 for name in listdir(directory):
249 filename = os.path.join(directory, name)
250 if is_exe(filename):
251 info("Running %s %s ..." % (filename, ' '.join(script_args)))
252 run_command_killable_and_import_envvars(filename, *script_args)
328def _walk_files():
329 # Check for a shortcut when running the tests interactively.
330 # If a BOTOCORE_TEST env var is defined, that file is used as the
331 # only test to run. Useful when doing feature development.
332 single_file = os.environ.get('BOTOCORE_TEST')
333 if single_file is not None:
334 yield os.path.abspath(single_file)
335 else:
336 for root, _, filenames in os.walk(TEST_DIR):
337 for filename in filenames:
338 yield os.path.join(root, filename)
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)
468def walk_files(dir: str, hidden: bool=False) -> Iterable[str]:
469 """Iterator over paths to non-directory files in dir.
471 The returned paths will all start with dir. In particular, if dir
472 is absolute, then all returned paths will be absolute.
474 If hidden=True, include hidden files and files inside hidden
475 directories."""
476 for root, dirs, files in os.walk(dir):
477 if not hidden:
478 del_hidden(dirs)
479 del_hidden(files)
480 for f in files:
481 yield os.path.join(root, f)

Related snippets