Python文件夹递归读取文件内容

import json
import requests
import re
import base64
import numpy as np
import os
 
# 遍历指定目录,显示目录下的所有文件名
def eachFiles(filepath):
    pathDir = os.listdir(filepath)
    for allDir in pathDir:
        child = os.path.join('%s\%s' % (filepath, allDir))
        if os.path.isfile(child):
    #判断文件扩展名是否为"*.req",是则处理,不是则跳过
            if child.endswith(".req"):
                   #读取文件内容
      readFile(child)
            continue
        eachFile(child)
 
# 遍历出结果 返回文件的名字
def readFile(files):
  #取文件的存放路径,不带文件名
    parent = os.path.dirname(os.path.realpath(files))
    #取文件名,不带文件存放路径
    filenames = os.path.basename(files)
    fopen = open(files, 'r', encoding='utf-8')  # r 代表read
    fileread = fopen.read()
    fopen.close()

if __name__ == "__main__":
    eachFiles(r"D:\project\work\接口测试")
posted @ 2021-08-23 20:24  沈墨白  阅读(375)  评论(0编辑  收藏  举报