22-Jan-2022
.
Admin
Hi Guys,
In this blog, I will learn you how to count total number of files and folder using python. We will show count number files and directory in perticuler folder in python. I would like to share with you python get total number of files and folder from your folder in python.
If you want to get files and folder in your perticuler directory using python then you can use bellow example for python.
This tutorial helps you learn how to quickly and easily count the number of files and the total number of directories on specific PATH recursively in a python application.
So let's explain it.
Example:
example.py
# import os module
import os
# your directory path
APP_FOLDER = '/var/www/php'
totalFiles = 0
totalDirectories = 0
for base, dirs, files in os.walk(APP_FOLDER):
print('Searching in : ',base)
for directories in dirs:
totalDirectories += 1
for Files in files:
totalFiles += 1
print('Total number of files',totalFiles)
print('Total Number of directories',totalDirectories)
print('Total:',(totalDirectories + totalFiles))
Run Example
python example.py
Output
('Searching in : ', '/var/www/php')
('Searching in : ', '/var/www/php/mehul')
('Searching in : ', '/var/www/php/category')
('Total number of files', 10)
('Total Number of directories', 2)
('Total:', 12)
It will help you....
#Python