Created
July 4, 2024 07:34
-
-
Save gustavz/b65c99092c88721dbe8a92a995390450 to your computer and use it in GitHub Desktop.
A Script to convert files to UTF-8 encoding (both file name and file content)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
import argparse | |
def convert_to_utf8(path, input_encoding): | |
path = Path(path) | |
if path.is_file(): | |
files = [path] | |
elif path.is_dir(): | |
files = path.glob('*') | |
else: | |
print(f"Invalid path: {path}") | |
return | |
for file in files: | |
if file.is_file(): | |
try: | |
content = file.read_text(encoding=input_encoding) | |
utf8_filename = file.with_name(file.name.encode('utf-8', errors='ignore').decode('utf-8')) | |
utf8_filename.write_text(content, encoding='utf-8') | |
if utf8_filename != file: | |
file.unlink() | |
print(f"Converted to UTF-8: {utf8_filename}") | |
except Exception as e: | |
print(f"Error converting {file}: {e}") | |
def main(): | |
parser = argparse.ArgumentParser(description="Convert files to UTF-8 encoding.") | |
parser.add_argument("path", help="Path to file or directory") | |
parser.add_argument("encoding", help="Input encoding") | |
args = parser.parse_args() | |
convert_to_utf8(args.path, args.encoding) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment