-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalkfiltalphdefl.py
More file actions
49 lines (38 loc) · 1.16 KB
/
walkfiltalphdefl.py
File metadata and controls
49 lines (38 loc) · 1.16 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
#!/usr/bin/python3
import os
import re
ALPHABET = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЪЫЭЮЯабвгдеёжзийклмнопрстуфхцчшщьъыэюя"
FILENAME_ENDINGS = [".txt.htm"]
IN_DIRPATH = "in"
IN_ENCODING = "cp1251"
OUT_DIRPATH = "out"
OUT_ENCODING = "utf8"
VERBOSE = True
def filtalphdefl(s):
return re.sub(" +", " ", re.sub(f"[^{ALPHABET}]", " ", s))
def filtfilename(filename):
for ending in FILENAME_ENDINGS:
if filename.endswith(ending):
return True
return False
def walk(in_dipath=IN_DIRPATH, out_dirpath=OUT_DIRPATH, verbose=VERBOSE):
try:
os.mkdir(out_dirpath)
except:
pass
n = 1
for (root, dirnames, filenames) in os.walk(in_dirpath):
for filename in filenames:
if filtfilename(filename):
with open(f"{root}/{filename}", "rb") as file:
content = file.read().decode(IN_ENCODING, errors="replace")
content = filtalphdefl(content)
with open(f"{out_dirpath}/{n:05}_{filename}", "wb") as file:
file.write(content.encode(OUT_ENCODING))
if verbose:
print(f"{n:05} : {filename}", flush=True)
n += 1
return (n - 1)
if __name__ == '__main__':
count = walk()
print(count)