10 examples of 'python count files in directory' in Python

Every line of 'python count files in directory' 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
16def file_count(source):
17 '''
18 Checks how many mov/mp4 files are in a dir.
19 '''
20 count = 0
21 for root, _, filenames in os.walk(source):
22 for files in filenames:
23 if files.lower().endswith(('.mov', '.mp4')):
24 count += 1
25 return count
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

13def get_nb_files(directory):
14 if not os.path.exists(directory):
15 return 0
16 cnt = 0
17 for r, dirs, files in os.walk(directory):
18 for dr in dirs:
19 cnt += len(glob.glob(os.path.join(r, dr + "/*")))
20 return cnt
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]
46def _count_files(self, path):
47 if os.path.isfile(path):
48 return 1
49
50 total = 0
51 for root, dirs, files in os.walk(path):
52 total += len([f for f in files if self.__is_robot_file(f) and not self.__is_excluded_path(root, f)])
53 if self.args.recursive:
54 # skip excluded directories
55 dirs[:] = [dir for dir in dirs if not self.__is_excluded_path(root, dir)]
56 else:
57 # stop traversing
58 del dirs[:]
59 return total
185def processonedir(dirname):
186 gdcmclasses = dir(gdcm)
187 subtotal = 0
188 for file in os.listdir(dirname):
189 #print file[-2:]
190 if file[-2:] != '.h': continue
191 #print file[4:-2]
192 gdcmclass = file[4:-2]
193 if gdcmclass in gdcmclasses:
194 print("ok:", gdcmclass)
195 else:
196 if not gdcmclass in blacklist:
197 print("not wrapped:",gdcmclass)
198 subtotal += 1
199 return subtotal
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
11def get_directory_size(path):
12 total_size = 0
13 for dirpath, dirnames, filenames in os.walk(path):
14 for f in filenames:
15 fp = os.path.join(dirpath, f)
16 total_size += os.path.getsize(fp)
17 return total_size
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)
146def test_get_file_count_with_empty_dir(self):
147 test_dir = self.get_temp_dir()
148 assert 0 == filetype.get_file_count(test_dir)
22@staticmethod
23def get_all_py_files():
24 """
25 Get all python files present in the project directory.
26
27 :return: list of python files in the project
28 :rtype: list
29 """
30 py_file_list = []
31 for root, _, files in os.walk(PROJ_DIR):
32 for file in files:
33 if '.py' == file[-3:]:
34 py_file_list.append(os.path.join(root, file))
35 return py_file_list

Related snippets