I have a the text of a book in txt format.
If a word is contained in specific_words_dict dictionary, I would like to replace that word with word_1 (cat with cat_1).
I wrote this code, but it doesn't replace the word in the file.
for filename in os.listdir(path):
with open(path+"/"+filename,'r+') as textfile:
for line in textfile:
for word in line.split():
if(specific_words_dict.get(word) is not None):
textfile.write(line.replace(word,word+"_1"))
What am I doing wrong?