传统windows下面的文件,很多老的项目中文件编码有gb2312的,也有unicode的,在vscode中修改的话经常会出错,因为写了个小脚本,把所有非unicode编码的文件统一改为unicode编码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| __author__ = 'whx' import os,sys import chardet import glob def convert( filename, in_enc = "GBK", out_enc="UTF-8-SIG" ): try: content = open(filename, 'rb').read() result = chardet.detect(content) coding = result.get('encoding') if "UTF" not in coding and "utf" not in coding : print ("转换 "+ coding + "to utf-8! " + filename) new_content = content.decode( coding ).encode(out_enc) open(filename, 'wb').write( new_content ) print (" done") else: print ("不用修改 "+coding + ":" +filename) except Exception as ex:
print (" error",ex)
filesAspx = glob.glob("D:\source\project/**/*.aspx",recursive=True) filesCs = glob.glob("D:\source\project/**/*.cs",recursive=True)
for file_path in filesAspx: convert(file_path) for file_path in filesAspx: convert(filesCs)
|