简体   繁体   中英

Python: list top-level directories with file count

I need to show the total file count for all top-level directories -- including the ones that have a file count of zero. Each top-level directory can contain subdirectories - need the total count listed next to top-level directory only.

cnt = 0 

for dirpath, dirnames, files in os.walk(FILES):
    filecount = len(files)
    cnt += filecount
    print(dirnames,": ",filecount)

How can I get the above to print something like:

  • top-level-dir1: 234

  • top-level-dir2: 0

  • top-level-dir3: 5

  • ....etc.

So total files, including what's in the nested subfolders, but print the total next to the top-level folders only.

UPDATE

    for directory in os.listdir(CONTRACTS):
        if os.path.isdir(directory):
            filecount = 0
            for dirpath, dirnames, files in os.walk(directory):
                filecount += len(files)
        print(directory,": ",filecount)

I'm close -- but this just shows file count as 1 for each.

You are resetting the filecount variable for each directory. Instead, you want the count to persist over each directory in CONTRACTS .

Also, os.listdir(CONTRACTS) only shows the immediate directory names in CONTRACTS; for the script to work in directories other than the current directory, you need to use os.path.join() to specify the full path when you call os.walk() .

Finally, as @TimRoberts says, you should use os.path.isdir() to check the output of os.listdir() , as it can also return files.

Something like this should do the trick:

import os

target = "target_directory"

for dir_name in os.listdir(target):
    dir_path = os.path.join(target, dir_name)
    if os.path.isdir(dir_path):
        file_count = 0
        for _, _, files in os.walk(dir_path):
            file_count += len(files)
        print(f"{dir_name}: {file_count}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM