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

Every line of 'loop through files in 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
9def process_dir(base_path, process_func):
10 for fp in iter_files(base_path):
11 process_func(fp)
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

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]
383
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):
387
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
395
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)
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)
126def find_files(base, languages=all_languages,
127 dir_ignore=default_dir_ignore,
128 file_ignore=default_file_ignore):
129 '''find all files in a directory and its subdirectories based on a
130 set of languages, ignore directories specified in dir_ignore and
131 files specified in file_ignore'''
132 if base[-1] != '/':
133 base += '/'
134
135 def update_dirs(dirs):
136 '''strip the ignored directories out of the provided list'''
137 index = len(dirs) - 1
138 for i,d in enumerate(reversed(dirs)):
139 if d in dir_ignore:
140 del dirs[index - i]
141
142 # walk over base
143 for root,dirs,files in os.walk(base):
144 root = root.replace(base, '', 1)
145
146 # strip ignored directories from the list
147 update_dirs(dirs)
148
149 for filename in files:
150 if filename in file_ignore:
151 # skip ignored files
152 continue
153
154 # try to figure out the language of the specified file
155 fullpath = os.path.join(base, root, filename)
156 language = lang_type(fullpath)
157
158 # if the file is one of the langauges that we want return
159 # its name and the language
160 if language in languages:
161 yield fullpath, language
6def find_files(directory, pattern, recursive=True):
7 files = []
8 for dirpath, dirnames, filenames in os.walk(directory, topdown=True):
9 if not recursive:
10 while dirnames:
11 del dirnames[0]
12
13 for filename in filenames:
14 filepath = os.path.join(dirpath, filename)
15 if fnmatch.fnmatch(filepath, pattern):
16 files.append(filepath)
17
18 return files
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)
468def walk_files(dir: str, hidden: bool=False) -> Iterable[str]:
469 """Iterator over paths to non-directory files in dir.
470
471 The returned paths will all start with dir. In particular, if dir
472 is absolute, then all returned paths will be absolute.
473
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