Last active
January 21, 2024 09:18
-
-
Save alpacato/235dd46b0486c9bbaaa11ee9875b4d3c to your computer and use it in GitHub Desktop.
Python script Changes files in folder recursive to another encoding.
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
# -*- coding: utf-8 -*- | |
import os | |
import codecs | |
# pip3 install cchardet | |
import cchardet as chardet | |
def changeEncoding(folder, encode_to): | |
for root, dirs, files in os.walk(folder): | |
path = root.split(os.sep) | |
for file in files: | |
if file.endswith('.php'): #filetypes | |
string_path = '/'.join([str(x) for x in path]) + "/" + file | |
read_data = None | |
info = '' | |
try: | |
source = open(string_path, 'rb') | |
read_data = source.read() | |
encoding_type = chardet.detect(read_data)['encoding'].lower() | |
source = codecs.open(string_path, 'r', encoding_type) #detect encoding | |
read_data = source.read() | |
info += " | en:" + encoding_type | |
info += " | cc:" + str(len(read_data)) | |
target = codecs.open(string_path, 'w+', encode_to, errors='replace') | |
target.write(str(read_data)) | |
except: | |
info += " | Error!" | |
print(string_path + info) | |
changeEncoding('folder_path', 'cp1251') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment