Count Files in a Directory

hokieghal99 hokiegal99 at hotmail.com
Mon Dec 22 11:11:49 EST 2003


Jay Dorsey wrote:
> On Sun, Dec 21, 2003 at 03:45:31PM -0800, 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. This code:
>>
> 
> 
> Do you want the total number of files under one directory, or
> the number of files under each directory?

Thanks for the tip... this bit of code seems to work:

def fs_object_count(path):
    file_count = 0
    dir_count = 0
    for root, dirs, files in os.walk(path):
       for fname in files:
          file_count += len(fname)
       for dname in dirs:
          dir_count += len(dname)
fs_object_count()





More information about the Python-list mailing list