26

How do I read the number of files in a specific folder using Python? Example code would be awesome!

9 Answers 9

32

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Down-voted as this answer is now out of date, to push the pathlib-based answer up. Do let me know if I shouldn't use down-votes this way.
32

pathlib, that is new in v. 3.4, makes life easier. The line labelled 1 creates a non-recursive list of the current folder, and the one labelled 2 creates a recursive list.

from pathlib import Path

import os
os.chdir('c:/utilities')

print (len(list(Path('.').glob('*')))) ## 1
print (len(list(Path('.').glob('**/*')))) ## 2

There are more goodies too. With these additional lines you can see both the absolute and relative file names for those items that are files.

for item in Path('.').glob('*'):
    if item.is_file():
        print (str(item), str(item.absolute()))

Result:

boxee.py c:\utilities\boxee.py
boxee_user_catalog.sqlite c:\utilities\boxee_user_catalog.sqlite
find RSS.py c:\utilities\find RSS.py
MyVideos34.sqlite c:\utilities\MyVideos34.sqlite
newsletter-1 c:\utilities\newsletter-1
notes.txt c:\utilities\notes.txt
README c:\utilities\README
saveHighlighted.ahk c:\utilities\saveHighlighted.ahk
saveHighlighted.ahk.bak c:\utilities\saveHighlighted.ahk.bak
temp.htm c:\utilities\temp.htm
to_csv.py c:\utilities\to_csv.py

1 Comment

Equivalent of the ## 1 line with iterdir is print( len(list(Path('.').iterdir())) )
7

You can use the glob module:

>>> import glob
>>> print len(glob.glob('/tmp/*'))
10

Or, as Mark Byers suggests in his answer, if you only want files:

>>> print [f for f in glob.glob('/tmp/*') if os.path.isfile(f)]
['/tmp/foo']
>>> print sum(os.path.isfile(f) for f in glob.glob('/tmp/*'))
1

2 Comments

It should be said, that os.listdir('.') includes hidden files (starting with a single dot), whereas glob('./*') does not.
@lunaryorn - If you want hidden files in the current directory, use glob('.*'). If you want everything including hidden files, use glob('.*') + glob('*').
6

Mark Byer's answer is simple, elegant, and goes along with the python spirit.

There's a problem, however: if you try to run that for any other directory than ".", it will fail, since os.listdir() returns the names of the files, not the full path. Those two are the same when listing the current working directory, so the error goes undetected in the source above.

For example, if you are at /home/me and you list /tmp, you'll get (say) ['flashXVA67']. You'll be testing /home/me/flashXVA67 instead of /tmp/flashXVA67 with the method above.

You can fix this using os.path.join(), like this:

import os.path
path = './whatever'
count = len([f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))])

Also, if you're going to be doing this count a lot and require performance, you may want to do it without generating additional lists. Here's a less elegant, unpythonesque yet efficient solution:

import os

def fcount(path):
    """ Counts the number of files in a directory """
    count = 0
    for f in os.listdir(path):
        if os.path.isfile(os.path.join(path, f)):
            count += 1
            
    return count


# The following line prints the number of files in the current directory:
path = "./whatever"
print fcount(path)

3 Comments

have a look at bstpierre's answer.
Indeed! Looks better than mine, and if you're reading this, check back on the first answer, an addition was made by Mark using walk() that addresses both of the problems I pointed out in a single line.
+1 for spotting the bug - I've updated my answer with your corrected version.
2

I think the easiest way is using pathlib and checking the length of the result of the iterdir() method. For example:

from pathlib import Path


search_path = Path('<relative or absolute path>')
n_files = len([*search_path.iterdir()])

If you want only certain types of files you can customize the list comprehension:

# only files
n_files = len([p for p in search_path.iterdir() if p.is_file()])

# only directories
n_files = len([p for p in search_path.iterdir() if p.is_dir()])

# only given extension
ext = '.png'
n_files = len([p for p in search_path.iterdir() if p.suffix==f'{ext}'])

Comments

1

Try this:

import os
for dirpath, dirnames, filenames in os.walk('./your/folder/path'):
    print(f'There are {len(dirnames)} directories and {len(filenames)} images in {dirpath}.')

The result would look like this:

There are 10 directories and 0 images in ./asl_data/photos.
There are 0 directories and 32 images in ./asl_data/photos\0.
There are 0 directories and 34 images in ./asl_data/photos\1.
There are 0 directories and 32 images in ./asl_data/photos\2.
There are 0 directories and 31 images in ./asl_data/photos\3.
There are 0 directories and 34 images in ./asl_data/photos\4.
There are 0 directories and 31 images in ./asl_data/photos\5.
There are 0 directories and 40 images in ./asl_data/photos\6.
There are 0 directories and 33 images in ./asl_data/photos\7.
There are 0 directories and 30 images in ./asl_data/photos\8.
There are 0 directories and 39 images in ./asl_data/photos\9.

Comments

0
total = len(filter(
            lambda f: os.path.isfile(os.path.join(path_to_dir, f)),
            os.listdir(path_to_dir)))

OR

total = sum([True for f in os.listdir(path_to_dir) if os.path.isfile(os.path.join([path_to_dir, f)])

Comments

0

recursive solution:

sum(len(fs) for _,_,fs in os.walk(os.getcwd()))

for current directory solution:

len(os.walk(os.getcwd()).next()[2])

Comments

0

print(len(os.listdir(r"your path")))

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.