count files in a directory
rbt
rbt at athop1.ath.vt.edu
Sat May 21 12:45:50 EDT 2005
Heiko Wundram wrote:
> 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!
>
Thanks! that works great... is there any significance to the underscores that you
used? I've always used root, dirs, files when using os.walk() do the underscores make
it faster... or more efficient?
More information about the Python-list
mailing list