【发布时间】:2016-09-27 16:30:30
【问题描述】:
我编写的这个程序不使用 os.walk()、glob 或 fnmatch,这是有意的。它查看目录以及该指定目录中的所有子目录和文件,并返回其中有多少文件+文件夹。
import os
def fcount(path):
count = 0
'''Folders'''
for f in os.listdir(path):
file = os.path.join(path, f)
if os.path.isdir(file):
file_count = fcount(file)
count += file_count + 1
'''Files'''
for f in os.listdir(path):
if os.path.isfile(os.path.join(path, f)):
count += 1
return count
path = 'F:\\'
print(fcount(path))
我得到的一个示例输出是目录 F 给了我700 总共 700 个文件和文件夹。
我现在想做的是使用这段代码,当然还有一些修改,调用fcount('F:\\')并返回一个集合(total files, folders)。
我想要的输出示例是:(700, 50)。 700 是 files + folders,50 只是 folders。
我不知道该怎么做。
【问题讨论】:
-
是的,使用元组。有什么问题?
-
@KarolyHorvath 不确定如何在这组代码中实现元组。
标签: python python-3.x recursion tuples subdirectory