计算目录和子目录中的文件夹数 [英] Count the number of folders in a directory and subdirectories

查看:113
本文介绍了计算目录和子目录中的文件夹数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以准确地告诉我目录中有多少文件以及其中的子目录。但是,我也正在调查确定同一目录及其子目录中有多少个文件夹...

I've got a script that will accurately tell me how many files are in a directory, and the subdirectories within. However, I'm also looking into identify how many folders there are within the same directory and its subdirectories...

我当前的脚本:

import os, getpass
from os.path import join, getsize
user = 'Copy of ' + getpass.getuser()
path = "C://Documents and Settings//" + user + "./"
folder_counter = sum([len(folder) for r, d, folder in os.walk(path)])
file_counter = sum([len(files) for r, d, files in os.walk(path)])
print ' [*] ' + str(file_counter) + ' Files were found and ' + str(folder_counter) + ' folders'

此代码为我提供以下打印内容: [*]找到147个文件和147个文件夹

This code gives me the print out of: [*] 147 Files were found and 147 folders.

这意味着 folder_counter 没有计算正确的元素。我该如何纠正它,以便 folder_counter 是正确的?

Meaning that the folder_counter isn't counting the right elements. How can I correct this so the folder_counter is correct?

推荐答案

I认为您想要以下内容:

I think you want something like:

import os

files = folders = 0

for _, dirnames, filenames in os.walk(path):
  # ^ this idiom means "we won't be using this value"
    files += len(filenames)
    folders += len(dirnames)

print "{:,} files, {:,} folders".format(files, folders)

请注意,这只会对 os.walk 进行一次迭代,这将使包含很多内容的路径的访问速度更快文件和目录。在我的Python目录上运行它可以使我:

Note that this only iterates over os.walk once, which will make it much quicker on paths containing lots of files and directories. Running it on my Python directory gives me:

30,183 files, 2,074 folders

与Windows文件夹属性视图显示的内容完全匹配。

which exactly matches what the Windows folder properties view tells me.

请注意,您的当前代码两次计算相同的数字,因为仅更改正在将调用返回的值之一重命名为 os.walk

Note that your current code calculates the same number twice because the only change is renaming one of the returned values from the call to os.walk:

folder_counter = sum([len(folder) for r, d, folder in os.walk(path)])
                        # ^ here          # ^ and here
file_counter = sum([len(files) for r, d, files in os.walk(path)])
                      # ^ vs. here     # ^ and here

尽管名称已更改,但您仍在计算相同的值(即,两者都是您正在使用的三个返回值中的第三个)! Python函数不知道是什么名字(如果有的话;例如,您可以执行打印列表(os.walk(path)))它们返回的值将被分配给它们,因此它们的行为当然不会改变。根据文档 os.walk 返回一个三元组(目录路径,目录名,文件名)以及用于此的名称,例如是否:

Despite that name change, you're counting the same value (i.e. in both it's the third of the three returned values that you're using)! Python functions do not know what names (if any at all; you could do print list(os.walk(path)), for example) the values they return will be assigned to, and their behaviour certainly won't change because of it. Per the documentation, os.walk returns a three-tuple (dirpath, dirnames, filenames), and the names you use for that, e.g. whether:

for foo, bar, baz in os.walk(...):

或:

for all_three in os.walk(..):

不会改变它。

这篇关于计算目录和子目录中的文件夹数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆