Python教程

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

本文主要是介绍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\接口测试")
这篇关于Python文件夹递归读取文件内容的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!