Created
July 17, 2023 04:30
-
-
Save cmsong-shina/0f1a20806409c87d0ada7261b93a1ccd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Generated by ChatGPT | |
import os | |
import sys | |
import glob | |
import chardet | |
import codecs | |
def change_file_encoding(file_path, target_encoding): | |
# Determine the current encoding of the file | |
with open(file_path, 'rb') as f: | |
content = f.read() | |
try: | |
detected_result = chardet.detect(content) | |
detected_encoding = detected_result['encoding'] | |
except Exception as e: | |
print(f"Failed to detect encoding for file: {file_path}. Error: {e}") | |
return | |
if detected_encoding is None: | |
print(f"Failed to detect encoding for file: {file_path}") | |
return | |
# Convert the file to the target encoding | |
try: | |
with codecs.open(file_path, 'r', encoding=detected_encoding) as f: | |
content = f.read() | |
with codecs.open(file_path, 'w', encoding=target_encoding) as f: | |
f.write(content) | |
print(f"Converted file: {file_path}") | |
except OSError as e: | |
print(f"Failed to read or write file: {file_path}. Error: {e}") | |
except LookupError as e: | |
print(f"Unsupported encoding: {e}") | |
except Exception as e: | |
print(f"Failed to convert file: {file_path}. Error: {e}") | |
def walk_and_convert_files(glob_pattern, target_encoding): | |
for file_path in glob.iglob(glob_pattern, recursive=True): | |
if os.path.isfile(file_path): | |
change_file_encoding(file_path, target_encoding) | |
if __name__ == '__main__': | |
if len(sys.argv) != 3: | |
print("Usage: python file_encoding_converter.py <glob_pattern> <target_encoding>") | |
sys.exit(1) | |
glob_pattern = sys.argv[1] | |
target_encoding = sys.argv[2] | |
if not glob.has_magic(glob_pattern): | |
print(f"Invalid glob pattern: {glob_pattern}") | |
sys.exit(1) | |
walk_and_convert_files(glob_pattern, target_encoding) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment