1. 程式人生 > >Python 遍歷資料夾中的檔案,並將檔案放到列表中

Python 遍歷資料夾中的檔案,並將檔案放到列表中

最經剛開始接觸Python,學習了用Python實現遍歷資料夾裡的檔案(只遍歷檔案不要目錄),並將這些檔案放到一個列表中

廢話不多說,直接上程式碼

def allDir(path):
    f=[]
    for root,dirs,files in os.walk(path):
        for filespath in files:
            print(os.path.join(root,filespath))
            f.append(os.path.join(root,filespath))

    print(f)
    return
(f)