count files in a directory

Heiko Wundram modelnine at ceosg.de
Sat May 21 04:45:32 EDT 2005


Am Samstag, 21. Mai 2005 06:25 schrieb James Stroud:
> This will work for your purposes (and seems pretty fast compared to the
> alternative):
>
> file_count = len(os.walk(valid_path).next()[2])

But will only work when you're just scanning a single directory with no 
subdirectories...!

The alternative (which will work regardless of subdirectories) is something 
like the following:

heiko at heiko ~ $ python
Python 2.4 (#1, Apr  3 2005, 00:49:51)
[GCC 3.4.3-20050110 (Gentoo Linux 3.4.3.20050110-r1, ssp-3.4.3.20050110-0, 
pie- on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> path = "/home/heiko"
>>> file_count = sum((len(f) for _, _, f in os.walk(path)))
>>> file_count
55579
>>>

HTH!

-- 
--- Heiko.
  see you at: http://www.stud.mh-hannover.de/~hwundram/wordpress/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050521/af90d19f/attachment.sig>


More information about the Python-list mailing list