#!/usr/bin/python3 import sys import codecs def test_utf8_encoding(filepath): try: with codecs.open(filepath, 'r', 'utf-8') as testfile: testfile.read() #print(f"{filepath} is openable with UTF-8 encoding.") except UnicodeDecodeError: print(f"{filepath} is not openable with UTF-8 encoding.") print(f"Converting {filepath} from ISO-8859-1 to UTF-8...") with codecs.open(filepath, 'r', 'iso-8859-1') as f: content = f.read() utf8_content = content.decode('iso-8859-1').encode('utf-8') with codecs.open(filepath, 'w', 'utf-8') as f: f.write(utf8_content) print(f"{filepath} has been converted to UTF-8.") if len(sys.argv) < 2: print("Please provide a file name as a command line argument.") sys.exit(1) filepath = sys.argv[1] test_utf8_encoding(filepath)