Count Files in a Directory

Martin v. Loewis martin at v.loewis.de
Sun Dec 21 19:15:42 EST 2003


hokiegal99 wrote:
> I'm trying to count the number of files within a directory, but I
> don't really understand how to go about it. 

You mean, you want the number of files in a directory? The number
of immediate files, or the number including files in nested
directories as well?

If you want the number of immediate files, you can use
os.listdir(path) to produce a list of files in a directory.
You can then use len(L) to compute the number of items in
a list. IOW,

print len(os.listdir(path))

does what you might want.

Regards,
Martin





More information about the Python-list mailing list