你如何计算文件夹中的子目录?

Kev*_*ark 1 python subdirectory

我想出了如何计算文件夹中的目录,但不知道如何编辑我的代码以递归计数子目录.任何帮助,将不胜感激.

到目前为止这是我的代码.

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
Run Code Online (Sandbox Code Playgroud)

R.F*_*son 5

我想你可能想用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)
Run Code Online (Sandbox Code Playgroud)