【发布时间】:2015-06-28 10:27:11
【问题描述】:
我有一个脚本可以准确地告诉我一个目录中有多少文件,以及其中的子目录。但是,我也在研究确定同一目录及其子目录中有多少个文件夹...
我当前的脚本:
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 Files were found and 147 folders。
这意味着folder_counter 没有计算正确的元素。我该如何纠正这个问题,以便 folder_counter 正确?
【问题讨论】:
-
为什么要重命名
os.walk中的第三个返回值来得到不同的结果? -
因为我是 Python 新手,希望它就这么简单
-
但这没有任何意义 - 该函数不知道您将返回的值分配给什么名称(如果有的话!)。
标签: python python-2.7 directory