Files
video-v1/vav2/convert_to_utf8.py
2025-09-23 02:26:17 +09:00

78 lines
2.2 KiB
Python

#!/usr/bin/env python3
import os
import chardet
import sys
from pathlib import Path
def detect_encoding(file_path):
"""파일의 인코딩을 감지합니다."""
try:
with open(file_path, 'rb') as f:
raw_data = f.read()
result = chardet.detect(raw_data)
return result['encoding']
except Exception as e:
print(f"인코딩 감지 실패: {file_path} - {e}")
return None
def convert_to_utf8(file_path):
"""파일을 UTF-8로 변환합니다."""
try:
# 현재 인코딩 감지
current_encoding = detect_encoding(file_path)
if not current_encoding:
print(f"건너뜀: {file_path} (인코딩 감지 실패)")
return False
# 이미 UTF-8인 경우 건너뜀
if current_encoding.lower() in ['utf-8', 'ascii']:
print(f"건너뜀: {file_path} (이미 UTF-8/ASCII)")
return True
# 파일 읽기
with open(file_path, 'r', encoding=current_encoding) as f:
content = f.read()
# UTF-8로 저장
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"변환 완료: {file_path} ({current_encoding} -> UTF-8)")
return True
except Exception as e:
print(f"변환 실패: {file_path} - {e}")
return False
def main():
"""메인 함수"""
current_dir = Path('.')
extensions = ['.h', '.cpp']
print("C/C++ 파일 UTF-8 변환을 시작합니다...")
print(f"대상 디렉토리: {current_dir.absolute()}")
print(f"대상 확장자: {', '.join(extensions)}")
print("-" * 50)
converted_count = 0
failed_count = 0
total_count = 0
# 모든 .h, .cpp 파일 찾기
for ext in extensions:
for file_path in current_dir.rglob(f'*{ext}'):
if file_path.is_file():
total_count += 1
if convert_to_utf8(file_path):
converted_count += 1
else:
failed_count += 1
print("-" * 50)
print(f"변환 완료!")
print(f"총 파일 수: {total_count}")
print(f"성공: {converted_count}")
print(f"실패: {failed_count}")
if __name__ == "__main__":
main()