46 lines
1.3 KiB
Python
Executable File
46 lines
1.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import sys
|
|
import argparse
|
|
|
|
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):
|
|
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)
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(
|
|
description='Recursively convert .cue and .log files from cp1251 to utf-8',
|
|
epilog='Example: python convert_encoding.py /path/to/directory'
|
|
)
|
|
parser.add_argument(
|
|
'directory',
|
|
type=str,
|
|
help='Path to target directory for conversion'
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
if not os.path.isdir(args.directory):
|
|
print(f"Error: '{args.directory}' is not a valid directory")
|
|
sys.exit(1)
|
|
|
|
process_directory(args.directory) |