music-tools/convert_file_encoding.py

33 lines
933 B
Python

import os
import sys
def convert_encoding(filepath):
try:
with open(filepath, 'r', encoding='cp1251', errors='strict') as f:
content = f.read()
with open(filepath, 'w', encoding='utf-8', errors='strict') as f:
f.write(content)
print(f"Success: {filepath}")
except UnicodeDecodeError:
print(f"Error: {filepath} - not cp1251 encoded")
except Exception as e:
print(f"Error: {filepath} - {str(e)}")
def process_directory(root_dir):
if not os.path.isdir(root_dir):
print(f"Error: '{root_dir}' is not a valid directory")
sys.exit(1)
for root, _, files in os.walk(root_dir):
for file in files:
if file.endswith(('.cue', '.log')):
file_path = os.path.join(root, file)
convert_encoding(file_path)
def execute(args):
process_directory(args.directory)