您的位置:首页 > 编程语言 > Python开发

python2.7 遍历目录 获取文件名与内容

2017-11-29 12:22 375 查看
# -*- coding: utf-8 -*-

import os.path
import glob
import os
import re
import codecs

if __name__ == '__main__':
jsonstr = ""
#获取指定格式文件列表
filelist =  glob.glob("E:\\Html5ChuanQi_Res\\client\\assets\\scene\\map\\*\\*.json")
#获取每个文件绝对路径
for single in filelist:
print single
#获取文件名
rule = r'(?=\\(\S[^\\]+?).json)'
keystr = re.findall(rule, single)
print keystr[0]
#读取文件内容
with open(single, "r") as f:
configjson = f.read()
#去除文件 BOM标识
if configjson[:3] == codecs.BOM_UTF8:
configjson = configjson[3:]
################################
#此可单独对读取数据进行需要操作#
################################

#合并所有内容
jsonstr = jsonstr + configjson
#写入测试文件
with open("test.json","w") as f:
f.write(jsonstr)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息