繁体   English   中英

试图获取目录中的文件总数

[英]Trying to get number of files total in a directory

我目前正在尝试读取一个目录并确定每个顶级目录中总共有多少文件,包括它们的每个子目录。 “文件夹检查扫描” function 应该进一步深入到子目录并通过调用自身来计算每个子目录中的文件,但它只计算第一级子目录中的文件。 有什么我想念的吗?

import os
import csv

def folders_check_scan(value):
    count = 0
    for y in os.scandir(value):
        if y.is_dir():
            folders_check_scan(y)
        elif y.is_file():
            count += 1
            
    return count



#function that takes summary of what is in repository
def list_files(startpath):
    #get number of folders in the first level of this directory
    print(len(next(os.walk(startpath))[1]))

    #gets top level folders in directory
    directory = next(os.walk(startpath))[1]
    print(startpath)
    directory_path = []
    for b in directory:
        newline = startpath + "\\" + b
        directory_path.append(newline)

    #call function to take record of number of files total in directory
    for x in directory_path:
        count = folders_check_scan(x)
        print(x)
        print(count)

看起来您没有存储递归调用的结果:

        if y.is_dir():
            count += folders_check_scan(y)
        elif y.is_file():
            count += 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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