首页 > 解决方案 > 如何在python中使用os.walk函数获取完整路径

问题描述

我正在使用 os.walk 查找目录,但它没有显示完整路径

代码:

for root, subdirs, files in os.walk(path):
    print(subdirs)

我将系统/值分配给路径。

预期输出:system/a/a

输出:一个

现在我可以使用glob.glob,但它列出了符号链接,我不想要那个

标签: python

解决方案


for root, dirnames, fnames in os.walk(path):
    print("I am looking in", root)
    print("These are the subdirectories:")
    for dirname in dirnames:
        print(os.path.join(root, dirname))

    print("These are the filenames:")
    for fname in fnames:
        print(os.path.join(root, fname))

推荐阅读