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

Every line of 'read 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
45def files_in(*dirs):
46 dir = os.path.join(*dirs)
47 files = os.listdir(dir)
48 return [os.path.join(dir, f) for f in files]

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

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
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)
22def files(path, pattern):
23 for file in os.listdir(path):
24 if (os.path.isfile(os.path.join(path, file)) and fnmatch.fnmatch(file, pattern)):
25 yield file
55def ReadFiles(self, dir_path):
56 """Read list of files.
58 :param str dir_path: Filesystem for directory to read.
59 :rtype: list of str
60 :return: List of files in directory.
62 """
63 return filter(os.path.isfile, self._Read(dir_path))
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 ]
31def _get_files(path):
32 for root, dirs, names in os.walk(path):
33 for name in names:
34 yield os.path.join(root, name)
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]
13 for filename in filenames:
14 filepath = os.path.join(dirpath, filename)
15 if fnmatch.fnmatch(filepath, pattern):
16 files.append(filepath)
18 return files
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
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

Related snippets