I'm trying to make a function where it reads a textfile and it inputs the repeated words once in another textfile. The problem is that this code just gives the last line repeated words.
import string def repeat_words(in_file, out_file): file_in = open(in_file, "r") file_out = open(out_file, "w") for lines in file_in: words = lines.lower().split() seen_words = [] repeat_words = [] for word in words: word = word.strip(string.punctuation) if word in seen_words and word not in repeat_words: repeat_words.append(word) seen_words.append(word) file_out.write(''.join(repeat_words) +'\n') file_in.close() file_out.close()inF = "nnn.txt"OutF = "Untitled-1.txt"repeat_words(inF, OutF)