[work] Python 递归遍历文件夹

向右看齐 2022-04-10 12:53 454阅读 0赞
  1. import os
  2. def get_log_path_dict():
  3. log_path = "/home/logs"
  4. for root, dirs, files in os.walk(log_path):
  5. log_path_dict = dict()
  6. for dir_name in dirs:
  7. dir_path = os.path.join(root, dir_name)
  8. log_path = dir_path + "/public.log"
  9. log_path_dict[dir_name] = log_path
  10. return log_path_dict
  11. print(get_log_path_dict())

亲测好用:

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

  1. import os
  2. dir = "e:\\"
  3. for root, dirs, files in os.walk(dir):
  4. for file in files:
  5. 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

发表评论

表情:
评论列表 (有 0 条评论,454人围观)

还没有评论,来说两句吧...

相关阅读