Skip to content

Instantly share code, notes, and snippets.

@thomashikaru
Created October 10, 2021 18:51
Show Gist options
  • Save thomashikaru/4abc4981a925d96bc89578792910d8f6 to your computer and use it in GitHub Desktop.
Save thomashikaru/4abc4981a925d96bc89578792910d8f6 to your computer and use it in GitHub Desktop.
from collections import Counter
import glob
# get list of filenames matching a pattern using glob
filenames = glob.glob("path/to/many/files/*.txt")
# create empty counter object
counts = Counter()
# loop over files, create a counter for each, and merge into counts
for file in filenames:
with open(file) as f:
counts += Counter(f.read().split())
# prints the 10 most common items in the Counter
# omit argument to print all items in descending order of frequency
print(counts.most_common(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment