Skip to content Skip to sidebar Skip to footer

How Do I Count Unique Words Of Text Files In Specific Directory With Python?

im writing a report and I need to count unique words of text files. My texts are in D:\shakeall and they're totally 42 files... I know some about Python, but I don't know what to d

Solution 1:

textfile=open('somefile.txt','r')
text_list=[line.split(' ') for line in textfile]
unique_words=[word for word in text_list if word not in unique_words]
print(len(unique_words))

That's the general gist of it


Solution 2:

import os
uniquewords = set([])

for root, dirs, files in os.walk("D:\\shakeall"):
    for name in files:
        [uniquewords.add(x) for x in open(os.path.join(root,name)).read().split()]

print list(uniquewords)
print len(uniquewords)

Post a Comment for "How Do I Count Unique Words Of Text Files In Specific Directory With Python?"