Saturday, 7 September 2013

Faster way to count files in Python

Faster way to count files in Python

I want to know how many files are in a folder (specifically a shared
network folder on windows if that makes a difference here).
I am using this code right now:
include os.path
def countFiles(path):
return len([f for f in os.listdir(path)
if os.path.isfile(os.path.join(path, f))])
It works fine when there are a few files in the folder, but it takes a
noticably long time in a directory with many files (say 4000). I am
running this frequently (files are being added every ~15 seconds) so the
slowdown is painful.
In my particular case, I know there aren't any subfolders, so I could skip
the os.path.isfile check, but I'd like to keep my solution general.
Frankly, I am surprised that there isn't a built in # of files function on
os.path.

No comments:

Post a Comment