"""遍歷目錄下的文件,將文件根據編碼utf-16 le讀出來,再以utf-8格式寫進去"""
#! python3
# encoding: utf-8
import os
import chardet
def strJudgeCode(str1):
return chardet.detect(str1)
def utf16leToUtf8(infile,outfile):
print(infile)
content="";
# 文件是utf-16 le編碼,故只讀取這種,也有不是這種編碼的,如果不成功就有可能拋出異常,因此爲了避免程序停止,需要捕獲異常
try:
with open(infile,"r",encoding='utf-16 le') as f:
content = f.read()
result = strJudgeCode(str.encode(content))
if(result['encoding'] == 'utf-8'): #此處可以去掉,因爲有部分是utf-8,故此部分不需要再轉換了
return
#print(content)
with open(outfile,"w",encoding='utf-8') as f1:
#f1.write(bytes.decode(content))
f1.write(content)
except UnicodeDecodeError:
print("UnicodeDecodeError err%s"%infile)
def listDirFile(dir):
#print(dir)
list = os.listdir(dir)
for line in list:
filepath = os.path.join(dir, line)
if os.path.isdir(filepath):
#print(filepath)
listDirFile(filepath)
else:
#print(line)
# 過濾出需要轉換的文件
if(filepath.endswith(".cpp") or filepath.endswith(".hpp") or filepath.endswith(".h")):
#if (filepath.endswith(".hpp")):
#converCode(filepath)
#utf16leToUtf8(filepath,dir+'/1'+line)
utf16leToUtf8(filepath, filepath)
if __name__ == '__main__':
print("hello")
listDirFile(r'E:\svn\cover')