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

来源:互联网 发布:淘宝买ipad 编辑:程序博客网 时间:2024/06/02 04:15
# -*- coding: utf-8 -*-import os.pathimport globimport osimport reimport codecsif __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) 

原创粉丝点击