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())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

python 获取一个文件夹内(包括子文件夹)所有文件的名字和路径

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

猜你喜欢

转载自blog.csdn.net/weixin_37887248/article/details/81011095