#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/7/10 13:52
# @Author : zhixiongg
# @Site :
# @File : utf8.py
# @Software: PyCharm
import os
from chardet import detect
def listdir(path, list_name): # 传入存储的list
for file in os.listdir(path):
file_path = os.path.join(path, file)
if os.path.isdir(file_path):
listdir(file_path, list_name)
elif (file_path.endswith('.cpp') or file_path.endswith('.h') or file_path.endswith('.c')): # 处理的文件后缀
list_name.append(file_path)
return list_name
filePath = input("请输入需要处理的文件夹路径:")
#filePath = 'C:\\Users\\zhixiongg\\Desktop\\EntierDbAPI'
fns = []
fns = listdir(filePath, fns)
for fn in fns:
with open(fn, 'rb+') as fp:
content = fp.read()
codeType = detect(content)['encoding']
if (codeType != 'utf-8' and content != b''): # 跳过空文件
print(fn, ":编码格式为:",codeType ,",请修改!")
print('检查完毕!')