This question has been asked many times. After spending some time reading the answers, I did some quick profiling to try out the various methods mentioned previously...
You could also try
with open('input.txt') as f:
# search_str is matched against each line in turn; returns on the first match:
print search_str in f
with search_str
ending with the proper newline sequence('\n'
or '\r\n'
). This should use little memory, as the file is read progressively. It should also be quite fast, since only part of the file is read.