[Novalug] Re: [Ma-linux] How to get the number of files in a given directory

Kevin Dwyer kevin at pheared.net
Thu Jan 10 09:53:00 EST 2008


On 1/10/08, Matt Thompson <matthew.thompson at nrl.navy.mil> wrote:
>
> # Count all files
> $ python -c "import os; print sum(len(files) for root, dirs, files in
> os.walk('.'))"
>
> # Count all directories
> $ python -c "import os; print sum(len(dirs) for root, dirs, files in
> os.walk('.'))"
>

os.walk descends into subdirectories so it's not exactly what you want.  Try
os.listdir('.').

(And yikes, python syntax has changed a bit since 2.3.)

-kpd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://calypso.tux.org/pipermail/novalug/attachments/20080110/c49e6fd7/attachment.html 


More information about the Novalug mailing list