以下Python代码计算总的文件,我有一个包含多个子目录的目录的数量。结果打印子目录名称以及它包含的文件数量。如何获取包含子目录的目录中的特定文件总数?
如何修改这个让:
- 它只是寻找一个特定的文件扩展名(即“* .SHP”)
- 它提供的每个“.SHP”文件这两个数子目录和所有 “.SHP” 的最终计数文件
下面是代码:
import os
path = 'path/to/directory'
folders = ([name for name in os.listdir(path)])
for folder in folders:
contents = os.listdir(os.path.join(path,folder))
print(folder,len(contents))
不是文本对方的副本,但相当不错。添加递归,你有你的答案。 – cwallenpoole
@cwallenpoole - 谢谢,我会看看=) – Joseph