使用python计算包含n个文件的文件夹数

2024-09-27 00:23:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含带有图像文件的文件夹的目录。我希望Python将它们的文件加起来的文件夹数返回给我,比如n

我试过的代码非常难看,包含很多循环,它给我的答案似乎不正确

import os


def countProcess(home_folder, noOfImg):
    noOfDir = 0
    noOfFiles = 0
    for base, dirs, file in os.walk(home_folder):
        dirs.sort()

        for directories in dirs:
            noOfDir += 1
            for _, _, files in os.walk(base + '/' + directories):
                for Files in files:
                    noOfFiles += 1
     
                    if noOfFiles == noOfImg:
                        return noOfDir


HOME_FOLDER = '/home/erfan/example/lfw/train'

print(countProcess(HOME_FOLDER, 100))

Tags: in文件夹homeforbaseosfilesfolder
3条回答

Wrt

I want Python to give me back the number of folders that their files add up to a number like n.

all files are in folders and there does not exist any subdirectories in the folders. There's just files containing images.

您可以这样做,而不需要自己递归其他dir,因为^{} already does that

def countProcess(home_folder, noOfImg):
    noOfDir = 0
    for _, _, files in os.walk(home_folder):
        if len(files) == noIfImg:
            noOfDir += 1
    return noOfDir

由于True的计算结果为1,而False的计算结果为0,因此它可以作为一个线性函数完成:

def countProcess(home_folder, noOfImg):
    return sum(len(files) == noIfImg for _, _, files in os.walk(home_folder))

编辑:如果不想对根目录中的文件计数,请添加一个标志以排除第一个循环,该循环用于作为参数提供给os.walk()的根目录:

def countProcess(home_folder, noOfImg):
    noOfDir = 0
    first = True
    for _, _, files in os.walk(home_folder):
        if not first and len(files) == noIfImg:
            noOfDir += 1
        first = False
    return noOfDir

这里有一个比os.walk()更慢的替代方法,即使用os.listdir()的简单递归。小心使用ret_dirs=[]它是一个可变变量,指针通过递归保持不变

import os

def get_num_nf(root, num, ret_dirs=[]):
    n_files = 0

    for entry in os.listdir(root):
        full_path = os.path.join(root, entry)

        if os.path.isdir(full_path):
            get_num_nf(full_path, num)

        elif os.path.isfile(full_path):
            n_files += 1

    if n_files >= num:
        ret_dirs.append(root)

    return ret_dirs

if __name__ == '__main__':

    dirs = get_num_nf('./env', 100)

    print(len(dirs))
    for path in dirs:
        print(path)

如果要计算仅包含n个文件的子文件夹(不包括父文件夹),请尝试此操作:

import os
import glob  

sum(len(glob.glob(i+'/*')) == n for i in glob.glob(ParentDir+'/*'))

相关问题 更多 >

    热门问题