How to find 500 most frequent words in 500 text files in python?
Question: I have 500 text files in one directory.I have to find 500 most frequent words
in all of the text files combined.How can I achieve that?

PS: I have searched a lot but could not find a solution.


Answer: Use
[`collections.Counter`](https://docs.python.org/3/library/collections.html#collections.Counter):

    
    
    import os
    from collections import Counter
    
    c, directory = Counter(), 'path_to_your_directory'
    
    for x in os.listdir(directory):
        fname = os.path.join(directory, x)
        if os.path.isfile(fname):
            with open(fname) as f:
                c += Counter(f.read().split())
    
    for word, _ in c.most_common(500):
        print(word)
    

Of course, it will read every file found in that directory. If that's not the
intended behavior, use [`glob.glob` or
`glob.iglob`](https://docs.python.org/3/library/glob.html) with the required
pattern instead of `os.listdir` (see Reut's comment to my answer).

