, :
1) The file is large . Then you cannot load it into memory. And you should read the file posts one by one and do your logic on each page. This will prevent a memory leak.
2) The file is small . Then you have the choice to follow the first case or read the file in memmory, close it, and then process it.
There is one more consideration : while the file is not closed, other processes cannot access it. This can be a problem if your logic is very long, in which case it is better to read and close the file.
source
share