vonzyy Posted March 30 Posted March 30 (edited) I have a lot of "utf-8" files and want to convert them to "utf-16 le bom" quickly. Is there a script that can do it? Edited May 4 by vonzyy
CaptJack Posted April 4 Posted April 4 Try spare into 2 files more than one. Atleast don't sell the machine translation
vonzyy Posted May 4 Author Posted May 4 On 4/4/2025 at 4:43 PM, CaptJack said: Try spare into 2 files more than one. Atleast don't sell the machine translation No, mate. I'm not selling it.
Moderators ikskoks Posted May 5 Moderators Posted May 5 This Python script should do the trick: import os def convert_to_utf16(input_path: str, output_path: str) -> None: print("Starting convert...") with open(input_path, 'rt', encoding='utf-8') as f_in: content = f_in.read() with open(output_path, 'wb') as f_out: f_out.write(b'\xff\xfe') f_out.write(content.encode('utf-16-le')) print("Convert success!") def convert_all_txt_in_folder(input_folder: str, output_folder: str) -> None: os.makedirs(output_folder, exist_ok=True) for filename in os.listdir(input_folder): if filename.lower().endswith('.txt'): input_path = os.path.join(input_folder, filename) base_name = os.path.splitext(filename)[0] output_filename = f"{base_name}_utf16le.txt" output_path = os.path.join(output_folder, output_filename) convert_to_utf16(input_path, output_path) print(f'Converted: {filename} -> {output_filename}') if __name__ == '__main__': convert_all_txt_in_folder('C:\\Users\\User\\Desktop\\UTF_FILES\\input', 'C:\\Users\\User\\Desktop\\UTF_FILES\\output') 1
vonzyy Posted May 6 Author Posted May 6 10 hours ago, ikskoks said: This Python script should do the trick: import os def convert_to_utf16(input_path: str, output_path: str) -> None: print("Starting convert...") with open(input_path, 'rt', encoding='utf-8') as f_in: content = f_in.read() with open(output_path, 'wb') as f_out: f_out.write(b'\xff\xfe') f_out.write(content.encode('utf-16-le')) print("Convert success!") def convert_all_txt_in_folder(input_folder: str, output_folder: str) -> None: os.makedirs(output_folder, exist_ok=True) for filename in os.listdir(input_folder): if filename.lower().endswith('.txt'): input_path = os.path.join(input_folder, filename) base_name = os.path.splitext(filename)[0] output_filename = f"{base_name}_utf16le.txt" output_path = os.path.join(output_folder, output_filename) convert_to_utf16(input_path, output_path) print(f'Converted: {filename} -> {output_filename}') if __name__ == '__main__': convert_all_txt_in_folder('C:\\Users\\User\\Desktop\\UTF_FILES\\input', 'C:\\Users\\User\\Desktop\\UTF_FILES\\output') Sorry, how to use the script. I have tried to put the script into python but the script still doesn't work.
Moderators ikskoks Posted May 6 Moderators Posted May 6 You execute it like any other Python script https://www.google.com/search?client=firefox-b-d&q=how+to+run+python+script python script.py
Sparagas Posted May 8 Posted May 8 On 5/6/2025 at 7:24 AM, vonzyy said: Sorry, how to use the script. I have tried to put the script into python but the script still doesn't work. On Windows you just double click it like normal .exe file
vonzyy Posted May 9 Author Posted May 9 23 hours ago, Sparagas said: On Windows you just double click it like normal .exe file I have done it but the script still doesn't work
vonzyy Posted May 9 Author Posted May 9 On 5/6/2025 at 11:43 PM, ikskoks said: You execute it like any other Python script https://www.google.com/search?client=firefox-b-d&q=how+to+run+python+script python script.py why does it appear like this?
Moderators ikskoks Posted May 9 Moderators Posted May 9 You should adjust your path. Just replace "User" with "MSI" and point to the directory that actually exist on your Desktop 😄
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now