Loading

获取文本文件编码与导出源代码

# 获取文件编码类型
import chardet
import os


# 获取文件编码
def get_encoding(file_path):
    with open(file_path, 'rb') as f:
        return chardet.detect(f.read())['encoding']


if __name__ == "__main__":
    rs = ''
    for i in os.listdir():
        if i.endswith('hpp') or i.endswith('cpp') or i.endswith('h') or i.endswith('rc'):
            print(get_encoding(i))
            with open(i, 'r', encoding=get_encoding(i)) as fp:
                for line in fp:
                    # 如果line是空白字符串或者全为空格
                    if line.isspace():
                        continue                        
                    rs += line
    with open('rs.txt','w+',encoding='utf8') as fp:
        fp.write(rs)
posted @ 2022-09-15 09:44  WindSnowLi  阅读(20)  评论(0编辑  收藏  举报