import re, operator
def count_words(filename):
with open(filename, 'rb') as fp:
data= memoryview(fp.read())
word_counts= {}
for match in re.finditer(br'\S+', data):
word= data[match.start(): match.end()]
try:
word_counts[word]+= 1
except KeyError:
word_counts[word]= 1
word_counts= sorted(word_counts.items(), key=operator.itemgetter(1), reverse=True)
for word, count in word_counts:
print(word.tobytes().decode(), count)
We could also use `mmap.mmap`. $ cat
^Z[1] + Stopped cat
$ kill %1 IFS=' ''
'
Hint: the spaces between the first two apostrophes are actually one <Tab>. IFS=' ''
'
echo hello >/tmp/"some_unique_prefix in tmp"
cat /tmp/some_unique_prefix*
fn="My CV.txt"
echo "I'm alive" >/tmp/$fn
cat /tmp/$fn
Of course this will still fail if there happens to be a filename with <Tab> in it.