How to count the number of files in a directory using Python
os.listdir()
will be slightly more efficient than using glob.glob
. To test if a filename is an ordinary file (and not a directory or other entity), use os.path.isfile()
:
import os, os.path
# simple version for working with CWD
print len([name for name in os.listdir('.') if os.path.isfile(name)])
# path joining version for other paths
DIR = '/tmp'
print len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])
(Python) - How to count number of files in a file with certain extension or name
Of course, as indicated in the earlier answer, len(glob.glob(...))
will tell you how many files matched the glob if you can rearticulate your requirement into a single wildcard pattern.
In the general case (for example, if you want to match .png
, .jpeg
, .JPEG
, etc), just increment a variable each time you see an occurrence.
# Stylistic fix: don't combine imports
import glob
import os
# This is our counter
count = 0
# Notice r'' raw string to preserve literal backslash
for file in os.listdir(r'D:\Train'):
if(file.endswith('jpg')):
print(file)
count += 1
print('Total:', count)
You can add a second variable fire_count
too; maybe then also rename the original variable for consistency.
total_count = 0
fire_count = 0
for file in os.listdir(r'D:\Train'):
if(file.endswith('jpg')):
print(file)
total_count += 1
if 'fire' in file:
fire_count += 1
print('Total:', total_count)
print('Fire files:', fire_count)
Count files in Folder by Python Programming
Try this instead
import os.path
path = os.getenv('HOME') + '/python'
num_files = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))])
This is because you are not in /home/python. You are actually in your home directory which is /home/$USER/python. If you do the following, in a terminal, you will see where you are.
cd ~/python && pwd
count number of file in a directory python
If you want to understand this syntax, I suggest you decompose it as follows:
os.walk(path)
will return a generator (basically an iterator)
<generator object walk at 0x7f5e5acbd4b0>
os.walk
is supposed to browse all levels of directories, you're asking for next()
to get the first level (and not the subdirectories)
os.walk(path).next()
This will return:
(
[0] -> The path you passed
[1] -> list of All the first level directories in your path
[2] -> list of All the first level files
)
In order to get the files, you'll ask for the element of index [2]
in your list
os.walk(path).next()[2]
And finally so you can count the number of these elements, you use len
(stands for length)
Here you go:
len(os.walk(path).next()[2])
Count number of files in subfolders in Python
Provided your pdfs end in the expected file extension, the solution is trivial:
import os
my_folder = "./" # your path here
count = 0
for root, dirs, files in os.walk(my_folder):
count += len([fn for fn in files if fn.endswith(".pdf")])
print(count)
It is a separate question if you want to determine the filetype by the contents directly; that is a harder problem.
Python count files in a directory and all its subdirectories
IIUC, you can just do
sum(len(files) for _, _, files in os.walk('path/to/folder'))
or perhaps, to avoid the len
for probably slightly better performance:
sum(1 for _, _, files in os.walk('folder_test') for f in files)
How do I read the number of files in a folder using Python?
To count files and directories non-recursively you can use os.listdir
and take its length.
To count files and directories recursively you can use os.walk
to iterate over the files and subdirectories in the directory.
If you only want to count files not directories you can use os.listdir
and os.path.file
to check if each entry is a file:
import os.path
path = '.'
num_files = len([f for f in os.listdir(path)
if os.path.isfile(os.path.join(path, f))])
Or alternatively using a generator:
num_files = sum(os.path.isfile(os.path.join(path, f)) for f in os.listdir(path))
Or you can use os.walk
as follows:
len(os.walk(path).next()[2])
I found some of these ideas from this thread.
How to count the number of files in subdirectories?
Look into parsing the output from os.walk
For example:
mydict = {}
for (root,dirs,files) in os.walk('testdir', topdown=False)
if len(files)>0:
mydict[root]=len(files)
print mydict
returns
{'testdir/EmployeeB/Jan': 2, 'testdir/EmployeeA/Feb': 2, 'testdir/EmployeeB/Feb': 1, 'testdir/EmployeeA/Jan': 3}
You could pretty easily parse those keys to generate the nested dictionary that you're looking for.
Related Topics
Python Overflowerror: Int Too Large to Convert to Float
Possible to Get User Input Without Inserting a New Line
How to Get the Name of an Object
Python: Pickle.Load() Raising Eoferror
Turn the Column Headers into the First Row and Row Headers into the First Column in Pandas Dataframe
Splitting Strings into Numbers (Python)
Xlsxwriter: How to Open an Existing Worksheet in My Workbook
Convert Float to Float Time in Python
Merging Two Dataframes With Different Lengths
Convert SQL Result to List Python
Remove Very First Row in Pandas
Splitting Dictionary Items into Smaller Dictionaries Based on Condition
How to Click on an Element from the Dropdown Menu Through Python and Selenium
Python Anaconda - How to Safely Uninstall
Change a Colour of a Pixel in Python
How to Convert a List of Dictionaries to Json in Python/Django
Creating a New Dataframe Column by Comparing Strings of Two Unequal Dataframes