[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

猜你喜欢

转载自blog.csdn.net/Scythe666/article/details/85997774
今日推荐