As mentioned you need to escape the '
character. The way to escape it is putting ''
. Like doen't
from os import listdir
with open("C:/Users/elle/Desktop/Archivess/test/rez.txt", "w") as f:
for filename in listdir("C:/Users/elle/Desktop/Archivess/test/sources/books/"):
with open('C:/Users/elle/Desktop/Archivess/test/freqs/books/' + filename) as currentFile:
text = currentFile.read()
text = text.strip().lower()
text = text.replace(".", "").replace(",", "").replace(""", "").replace("'", "") # replace all .,"'
words = text.split(" ") # split the text
unique_words = set(words)
count_dict = {}
for each_word in words:
if(each_word in count_dict):
count_dict[each_word] += 1
else:
count_dict[each_word] = 1
for k in count_dict:
f.write('The word' + k +'excist in the file ' + filename[:-4] + ' for ' + str(count_dict[k]) + ' number of times' '
')
# if ('beautiful' in text):
# f.write('The word excist in the file ' + filename[:-4] + '
')
# else:
# f.write('The word doen't excist in the file' + filename[:-4] + '
')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…