I may recommend to install and use the_silver_searcher.
In my test it searched ~ 1GB text file with ~ 29 million lines and found hundreds of searched word entries in only 00h 00m 00.73s, i.e. LESS than a second!
Here is Python 3 code which uses it to search for word and count number of times it was found:
import subprocess
word = "some"
file = "/path/to/some/file.txt"
command = ["/usr/local/bin/ag", "-wc", word, file]
output = subprocess.Popen(command, stdout=subprocess.PIPE).stdout.read()
print("Found entries:", output.rstrip().decode('ascii'))
This version searches for word and prints line numbers + actual text were the word was found:
import subprocess
word = "some"
file = "/path/to/some/file.txt"
command = ["/usr/local/bin/ag", "-w", word, file]
output = subprocess.Popen(command, stdout=subprocess.PIPE)
for line in output.stdout.readlines():
print(line.rstrip().decode('ascii'))