1. 程式人生 > >[work] Python 遞迴遍歷資料夾

[work] Python 遞迴遍歷資料夾

import os

def get_log_path_dict():
    log_path = "/home/logs"
    for root, dirs, files in os.walk(log_path):
        log_path_dict = dict()
        for dir_name in dirs:
            dir_path = os.path.join(root, dir_name)
            log_path = dir_path + "/public.log"
            log_path_dict[dir_name] = log_path
        return log_path_dict

print(get_log_path_dict())

親測好用:

python 獲取一個資料夾內(包括子資料夾)所有檔案的名字和路徑

import os
dir = "e:\\"
for root, dirs, files in os.walk(dir):
    for file in files:
        print os.path.join(root,file)

1、python遍歷一個目錄,輸出所有的檔名

http://blog.csdn.net/Thinking_boy1992/article/details/70239367

2、使用Python處理目錄(一):列印目錄下的檔名

http://blog.csdn.net/mishifangxiangdefeng/article/details/50544270