#pip install chardet
import os
import chardet
def convert_to_utf8(file_path):
try:
with open(file_path, 'rb') as f:
content = f.read()
encoding = chardet.detect(content)['encoding']
if encoding and not 'utf-8' in encoding.lower() :
contents = content.decode(encoding)
#contents = contents.encode('UTF-8')
#contents = contents.replace('\r\n\r\n','\r\n')
with open(file_path, 'w', encoding='UTF-8',newline='\n') as utf8_file:
utf8_file.write(contents)
print(f"Converted {file_path} to UTF-8")
except Exception as e:
print(f"Error processing {file_path}: {e}")
def convert_folder(folder_path):
for root, dirs, files in os.walk(folder_path,topdown=False):
for file in files:
file_path = os.path.join(root, file)
if 'svn' in root :
continue
elif 'vs' in root:
continue
elif file == 'python.py':
continue
elif not file.endswith('.cs'):
continue
convert_to_utf8(file_path)
if __name__ == "__main__":
target_folder = "./" # 대상 폴더 경로로 바꾸세요
convert_folder(target_folder)