Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python скрипт для рекурсивного перевода файлов(можно указать несколько типов файлов) из кодировки CP1251 в UTF8
import os, codecs
for (dir, _, files) in os.walk("./"):
for f in files:
path = os.path.join(dir, f)
if f.endswith(('php')):
if os.path.exists(path):
data = open(path, "rb").read()
print(path)
if data.startswith(codecs.BOM_UTF8):
continue
try:
data = data.decode("cp1251")
except UnicodeDecodeError:
continue
data = data.encode("utf-8")
open(path, "wb").write(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment