tags:

views:

94

answers:

3

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

+15  A: 

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.

Mark Byers
A: 

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
bstpierre
It should be said, that `os.listdir('.')` includes hidden files (starting with a single dot), whereas `glob('./*')` does not.
lunaryorn
@lunaryorn - If you want hidden files in the current directory, use `glob('.*')`. If you want everything including hidden files, use `glob('.*') + glob('*')`.
bstpierre
+1  A: 

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 your 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)
Santiago Lezica
have a look at bstpierre's answer.
SilentGhost
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.
Santiago Lezica
+1 for spotting the bug - I've updated my answer with your corrected version.
Mark Byers