Created
March 17, 2017 17:53
-
-
Save iredun/08293e30cc868dd954c8718a25b24709 to your computer and use it in GitHub Desktop.
Python скрипт для рекурсивного перевода файлов(можно указать несколько типов файлов) из кодировки CP1251 в UTF8
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
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