I wrote this code, and I have trouble writing the results. The current code only writes the last word, from a file with 50,000 words!
Code:
filename = 'words.txt'
try:
with open('G:\\Nalpha.txt', 'r') as fileobject:
contents = fileobject.read()
except FileNotFoundError:
message = 'Sorry, the file ' + filename + ' connot be found.'
print(message)
else:
words = contents.split()
number_words = len(words)
print('The file ' + filename + ' has approximatley ' + str(number_words) + ' words.')
alphabet = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')
unique_words = []
'''Word Frequency'''
words = contents.split()
for word in words:
if word not in unique_words:
unique_words.append(word)
else:
pass
for i in unique_words:
word_frequencey = words.count(i)
print(i, ':', word_frequencey)
textfile = open('G:\\Nalphadone.txt', 'w')
textfile.write(word)
textfile.close()