Discuss / Python / 不知道为什么只能遍历出第一目录

不知道为什么只能遍历出第一目录

Topic source
import os
def f(path='.'):
    p=os.path.abspath(path)
    for v in [x for x in os.listdir(p) if os.path.isdir(x)]:
            cpth=os.path.join(p,v) #子目录的绝对路径
            print(cpth)
            f(cpth) #递归,这样就能找出子母录的子目录的子目录的......

漫步立冬

#2 Created at ... [Delete] [Delete and Lock User]

os.path.isdir() 函数需要绝对路径。如下修改即可。

for v in [x for x in os.listdir(p) if os.path.isdir(os.path.join(p,x))]:

  • 1

Reply