-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.py
executable file
·72 lines (58 loc) · 1.53 KB
/
tools.py
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
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
import codecs
import os
from glob import glob
from unidecode import unidecode
import os
def get_all_files():
"""
:returns: list with all textes
"""
return glob("./texts/*/*.txt")
def files():
"""
:yields: file -- current text from all texts
"""
files = get_all_files()
for filename in files:
with codecs.open(filename, "r+", encoding="utf-8") as f:
yield f
def remove_unicode():
"""
Removes unicode characters
"""
for f in files():
new_text = unidecode(f.read())
f.seek(0)
f.truncate()
f.write(new_text)
def count_all():
"""
:returns: int -- number of files
"""
return len(get_all_files())
def remove_email_info(sentence=None):
"""
Removes files with sentence
"""
if not sentence:
sentence = "An email with a link to: Thanks for sharing About.com with others!"
files_to_delete = []
for f in files():
if sentence.split() == f.read().split():
files_to_delete.append(f.name)
for filename in files_to_delete:
os.remove(filename)
#TODO: Kacper
def renumber_files(root_dir):
for root, subFolders, files in os.walk(root_dir):
counter = 0
for file in files:
counter+=1
old_file = os.path.join(root, file)
new_file = os.path.join(root, str(counter) + ".txt")
os.rename(old_file, new_file)
if __name__ == '__main__':
remove_unicode()
print(count_all())
# remove_email_info()