Skip to content

Instantly share code, notes, and snippets.

@iredun
Created March 17, 2017 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iredun/08293e30cc868dd954c8718a25b24709 to your computer and use it in GitHub Desktop.
Save iredun/08293e30cc868dd954c8718a25b24709 to your computer and use it in GitHub Desktop.
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