python文件读取编码笔记

import os
    pathloc = 'C:\\Users\\lw\\Desktop\\课件'
    try:
        with open("log_logo", "w+", encoding="utf-8") as f:
            for root, dirs, files in os.walk(pathloc, topdown=False):
                # print('root:',root)
                # print('dirs:',dirs)
                # print('files:',files)
                tmpdir = root
                for filename in files:
                    if filename.endswith("java"):
                        filepath = os.path.join(tmpdir, filename)  #需要拼接路径
                        try:
                            with open(filepath, 'r', encoding="gbk")as f1:
                            # with open(filepath, 'r', encoding="utf-8")as f1:
                                s = f1.read()
                                # print(s)
                                f.write(s)
                        except:
                            with open(filepath, 'r', encoding="UTF-8")as f1:
                            # with open(filepath, 'r', encoding="utf-8")as f1:
                                s = f1.read()
                                # print(s)
                                f.write(s)
    except:
        pass

网站题目:python文件读取编码笔记
转载注明:http://csruizhi.cn/article/ihhosh.html

其他资讯

Copyright © 2007-2024 成都优众联杰科技有限公司 All Rights Reserved 蜀ICP备2024116266号
友情链接: 网站建设 成都网站设计 品牌网站建设 攀枝花网站设计 手机网站制作 成都网站建设 企业网站设计 响应式网站设计 定制网站设计 成都企业网站制作 成都网站建设 成都网站建设流程 网站建设方案 宜宾网站设计 成都商城网站建设 成都网站建设 成都网站制作 成都企业网站建设 专业网站设计 成都网站建设 上市集团网站建设 阿坝网站设计