繁体   English   中英

如何在 python 中使用 os.walk function 获取完整路径

[英]how to get full path with os.walk function in python

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

代码:

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

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

预期 output:系统/a/a

Output:一个

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

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))

引用文档

要获取 dirpath 中文件或目录的完整路径(以 top 开头),请执行os.path.join(dirpath, name)

把它放在一起,你会得到以下结果:

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

这是你应该做的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM