(May-05-2021, 07:25 PM)tester_V Wrote: The files are big and many, I do not want to do 'readlines' and if I'll do open each file then it is like doing 'for' loop, and I do not need 'any()'
I thought I can test each file as a whole file without checking each line.
Thank you.
If the files aren't large, you can just read the entire file into a string and then check if you have a string match. There is no loop necessary.
with open(filename, "r") as f: if matchstring in f.read(): ...But if the files are large, you don't want to load in the entire file into a single string. That implies that you will need to loop over chunks of the file. If it's a "sane" text file, you can assume newlines every so often and let the file iterator hand you one line at a time. Loop over each and exit when you get your first match.
any()
would be nice about doing that.with open(filename, "r") as f: # f is the file iterator. The expression inside the parens is a generator comprehension, so # the entire file is not read into memory, just each line is matched. any() will exit after any # match is successful and doesn't have to read the rest of the file. if any(matchstring in line for line in f): ...