Skip to content

Instantly share code, notes, and snippets.

@Holit
Last active November 12, 2022 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Holit/9e86d5736b971c29c128969a9857530f to your computer and use it in GitHub Desktop.
Save Holit/9e86d5736b971c29c128969a9857530f to your computer and use it in GitHub Desktop.
将文件夹下的.c、.h、.asm文件转换为utf-8格式。使用方法:python run.py [path]
#要循环所有子文件夹,请使用bat指令
#for /d %i in (*.*) do (python charcode-trans.py %i)
import os
import sys
import codecs
import chardet
import sys
#将路径下面的所有文件,从原来的格式变为UTF-8的格式
def convert(file, in_enc="GBK", out_enc="UTF-8"):
"""
该程序用于将目录下的文件从指定格式转换到指定格式,默认的是GBK转到utf-8
:param file: 文件路径
:param in_enc: 输入文件格式
:param out_enc: 输出文件格式
:return:
"""
in_enc = in_enc.upper()
out_enc = out_enc.upper()
try:
print("convert [ " + file.split('\\')[-1] + " ].....From " + in_enc + " --> " + out_enc )
f = codecs.open(file, 'r', in_enc)
new_content = f.read()
codecs.open(file, 'w', out_enc).write(new_content)
# print (f.read())
except IOError as err:
print("I/O error: {0}".format(err))
def list_folders_files(path):
"""
返回 "文件夹" 和 "文件" 名字
:param path: "文件夹"和"文件"所在的路径
:return: (list_folders, list_files)
:list_folders: 文件夹
:list_files: 文件
"""
list_folders = []
list_files = []
for file in os.listdir(path):
file_path = os.path.join(path, file)
if os.path.isdir(file_path):
list_folders.append(file)
else:
list_files.append(file)
return (list_folders, list_files)
if __name__ == "__main__":
path = sys.argv[1]
(list_folders, list_files) = list_folders_files(path)
print("Path: " + path)
for fileName in list_files:
filePath = path + '\\' + fileName
if '.c' in fileName or '.h' in fileName or '.asm' in fileName:
with open(filePath, "rb") as f:
data = f.read()
codeType = chardet.detect(data)['encoding']
convert(filePath, codeType, 'UTF-8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment