Created
October 10, 2021 18:51
-
-
Save thomashikaru/4abc4981a925d96bc89578792910d8f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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