fname = input("Enter the name of the file:") infile=open(fname,'a') infile.write("append the sentences:") infile = open(fname, 'r') wordcount={} lines = 0 for word in infile.read().split(): if word not in wordcount: wordcount[word] = 1 else: wordcount[word] += 1 print (word,wordcount) infile.close();