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号
友情链接: 成都网站制作 企业网站设计 成都网站建设 定制级高端网站建设 网站建设方案 企业手机网站建设 温江网站设计 响应式网站设计方案 成都网站建设公司 手机网站制作 手机网站建设 成都企业网站建设公司 四川成都网站制作 成都网站制作 成都h5网站建设 成都网站制作 网站制作 重庆网站建设 网站设计 重庆网站建设 网站建设公司 成都网站建设公司