-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreencode.py
More file actions
60 lines (54 loc) · 2.1 KB
/
reencode.py
File metadata and controls
60 lines (54 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import sys
import shutil
import codecs
from pathlib import Path
if (len(sys.argv) != 3):
print("Перекодировка файлов в UTF-8 из CP1251")
print(" reencode.py source target")
print(" source : папка с объектами для перекодировки")
print(" target : уелдевая папка")
exit
mainFolder = sys.argv[1]
outputRepo = sys.argv[2]
print("Источник: " + mainFolder)
print("Цель: " + outputRepo)
pathlist = Path(mainFolder).glob('**/*.pls')
BLOCKSIZE = 1048576
for path in pathlist:
strInputPath = str(path)
strOutputPath = outputRepo + "/" + strInputPath;
print(strInputPath)
try:
fh = codecs.open(strInputPath, 'r', encoding='utf-8')
fh.readlines()
fh.seek(0)
except UnicodeDecodeError:
print('File is Windows-1251 encoded')
strOutputDirs, _ = os.path.split(strOutputPath)
if (not os.path.exists(strOutputPath)):
if not os.path.exists(strOutputDirs):
print("Creating: " + strOutputDirs)
os.makedirs(strOutputDirs)
with codecs.open(strInputPath, "r", "CP1251") as sourceFile:
with codecs.open(strOutputPath, "w", "utf-8") as targetFile:
while True:
print("Saving: " + strOutputPath)
contents = sourceFile.read(BLOCKSIZE)
if not contents:
break
targetFile.write(contents)
else:
print("File already reencoded")
else:
print('File is UFT-8 encoded')
if (not os.path.exists(strOutputPath)):
print("Copying: " + strInputPath)
strOutputDirs, _ = os.path.split(strOutputPath)
if (not os.path.exists(strOutputPath)):
if not os.path.exists(strOutputDirs):
print("Creating: " + strOutputDirs)
os.makedirs(strOutputDirs)
shutil.copyfile(strInputPath, strOutputPath)
else:
print("File already reencoded")