-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtake_5_words_only.py
33 lines (24 loc) · 980 Bytes
/
take_5_words_only.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
## program which checks all *.txt files in working directory and parse all words in them.
## If the words are of lnght of 5, then the words are stored in a file for further manipulations.
import glob
import os
# os.chdir(r'directory where the files are located')
myFiles = glob.glob('*.txt')
print(myFiles)
word_list_unique = set()
for file in myFiles:
contents = list()
with open(file, 'r') as f:
contents = f.read().splitlines()
print(file)
word_list_unique.update(contents)
# print(contents)
print(f"All unique words are: {len(word_list_unique)}")
word_list_5 = [word for word in word_list_unique if len(word) == 5]
word_list_unique = list(word_list_unique)
print(f"All unique words with length of 5 are: {len(word_list_5)}")
with open('5_list.txt', 'w') as f:
for word in word_list_5:
f.write("%s\n" % word)
print("All words with lenght of 5 are saved in file '5_list.txt'")
print(word_list_5)