簡體   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