In Python, we can count the number of files in a directory easily with the listdir() function from the Python os module.
import os
print(len(os.listdir(r"examples")))
#Ouput:
5
Note, the listdir() function returns a list of all names in a directory. To just get all files, you can check each name with the isdir() function.
import os
path = r"examples"
count = 0
for x in os.listdir(path):
if !os.path.isdir(os.path.join(path,x)):
count = count + 1
print(count)
#Output:
3
If you want just the files of a certain extension, you can use the endswith() function to check the extension of each file.
import os
path = r"examples"
count = 0
for x in os.listdir(path):
if x.endswith(".py"):
count = count + 1
print(count)
#Output:
2
When working with file systems, it can be useful to be able to get the number of files in a particular directory.
The Python os module provides us with a number of great functions to be able to perform many operating system tasks.
With the os module, we can count the number of files in a particular directory easily.
The listdir() function takes in a path and gets a list of all of the files in that directory. We can then find the length of that list to get the number of files in the directory.
Below is an example of how to get the number of files in a directory using Python.
import os
print(len(os.listdir(r"/examples")))
#Ouput:
5
listdir() returns all names in a directory, so the length of listdir() will count the number of items in a directory. To just get the number of files, and ignore the subdirectories, you can check each name with the isdir() function.
import os
def countOnlyFiles(path):
count = 0
for x in os.listdir(path):
if !os.path.isdir(os.path.join(path,x)):
count = count + 1
return count
print(countOnlyFiles(r"/examples"))
#Output:
3
To just count the number of files of a certain extension, we can loop over the files and check the extensions with the help of the endswith() function.
import os
def countPyFiles(path):
count = 0
for x in os.listdir(path):
if x.endswith(".py"):
count = count + 1
return count
print(countPyFiles(r"/examples"))
#Output:
2
Counting the Number of Files in a Folder and All Subfolders in Python
Another great os module function is the os module walk() function. The walk() function returns the entire tree of folders and subfolders given a path.
We can use the walk() function to get all folders and subfolders, and then iterate over the returned object to count the number of files in each folder and subfolder.
Let’s say we have the following folder structure.
examples
-- code1.py
-- code2.py
-- examples1
---- code1_1.py
-- examples2
---- code2_1.py
---- code2_2.py
---- code2_3.py
In the 3 folders, we have a few files.
Let’s use the os walk() function to get the count of the files in each of the folders of our directory.
Below is the Python code which will allow you to get the number of files in each of the folders and subfolders of a given path.
import os
def getAllFiles(path):
print(path)
print(len(os.listdir(path)))
for root, dirs, files in os.walk(path):
for name in dirs:
print(os.path.join(root,name))
print(len(os.listdir(os.path.join(root,name))))
getAllFiles(r"examples")
#Output:
examples
4
examplesexamples1
1
examplesexamples2
3
From above, we know that listdir() treats all names as files. To filter out the subfolders, we can use the isdir() function.
import os
def countOnlyFiles(path):
count = 0
for x in os.listdir(path):
if !os.path.isdir(os.path.join(path,x)):
count = count + 1
return count
def getAllFiles(path):
print(path)
print(countOnlyFiles(path))
for root, dirs, files in os.walk(path):
for name in dirs:
print(os.path.join(root,name))
print(countOnlyFiles(os.path.join(root,name)))
getAllFiles(r"examples")
#Output:
examples
2
examplesexamples1
1
examplesexamples2
3
If you want to get just files of a certain extension, then you can use the endswith() function.
import os
def countPyFiles(path):
count = 0
for x in os.listdir(path):
if x.endswith(".py"):
count = count + 1
return count
def getAllFiles(path):
print(path)
print(countPyFiles(path))
for root, dirs, files in os.walk(path):
for name in dirs:
print(os.path.join(root,name))
print(countPyFiles(os.path.join(root,name)))
getAllFiles(r"examples")
#Output:
examples
2
examplesexamples1
1
examplesexamples2
3
Hopefully this article has been useful for you to understand how to count the number of files in a directory with Python.