我想出了如何对文件夹中的目录进行计数,但不确定如何编辑我的代码以递归地对子目录进行计数。任何帮助将不胜感激。
到目前为止,这是我的代码。
def nestingLevel(path):
count = 0
for item in os.listdir(path):
if item[0] != '.':
n = os.path.join(path,item)
if os.path.isdir(n):
count += 1 + nestingLevel(n)
return count
请您参考如下方法:
我想你可能想使用 os.walk :
import os
def fcount(path):
count1 = 0
for root, dirs, files in os.walk(path):
count1 += len(dirs)
return count1
path = "/home/"
print fcount(path)
