python 脚本 遍历目录,并把目录下的非utf-8文件改成utf8

发布于:2025-06-28 ⋅ 阅读:(18) ⋅ 点赞:(0)

从网上下载的qt项目我本地编译里面经常包含中文,提示编译不过,实际上以前经常手动转,发觉还是用脚本不,毕竟这次下的有点大,我只改.h .cpp

#python D:\python\filetoUtf.py  E:\EasyCanvas-master\EasyCanvas-master

import os
import codecs
import argparse
import sys

def convert_to_utf8_bom(file_path):
    """将文件转换为 UTF-8 with BOM 格式"""
    try:
        # 读取原始文件内容
        with open(file_path, 'rb') as f:
            content_bytes = f.read()
        
        # 尝试检测原始编码
        try:
            content_text = content_bytes.decode('utf-8')
            # 如果已经是UTF-8且包含BOM,跳过
            if content_bytes.startswith(codecs.BOM_UTF8):
                print("✓ " + file_path + " 已经是 UTF-8 with BOM")
                return
        except UnicodeDecodeError:
            # 尝试其他常见编码
            try:
                content_text = content_bytes.decode('gbk')
            except:
                try:
                    content_text = content_bytes.decode('latin1')
                except:
                    content_text = content_bytes.decode('utf-8', errors='replace')
        
        # 添加BOM并写入文件
        with open(file_path, 'w', encoding='utf-8-sig') as f:
            f.write(content_text)
        
        print("★ " + file_path + " 已成功转换为 UTF-8 with BOM")
    
    except Exception as e:
        print("✗ 处理 " + file_path + " 时出错: " + str(e))

def process_directory(directory):
    """递归处理目录中的所有 C++ 文件"""
    for root, _, files in os.walk(directory):
        for file in files:
            if file.lower().endswith(('.cpp', '.h', '.hpp', '.cxx', '.cc')):
                file_path = os.path.join(root, file)
                convert_to_utf8_bom(file_path)

if __name__ == "__main__":
    # 检查Python版本
    if sys.version_info < (3, 0):
        print("错误: 需要 Python 3.x 版本")
        sys.exit(1)
    
    parser = argparse.ArgumentParser(description='将 C++ 文件转换为 UTF-8 with BOM 编码')
    parser.add_argument('directory', nargs='?', default='.', 
                        help='要处理的目录 (默认为当前目录)')
    args = parser.parse_args()
    
    target_dir = os.path.abspath(args.directory)
    
    print("=" * 60)
    print("开始处理目录: " + target_dir)
    print("=" * 60)
    process_directory(target_dir)
    print("=" * 60)
    print("处理完成!建议在 .pro 文件中添加: win32-msvc*: QMAKE_CXXFLAGS += /utf-8")
    print("=" * 60)

执行脚本


网站公告

今日签到

点亮在社区的每一天
去签到