The file will be closed. Using with or explicit closing in many cases is unnecessary.
Python closes the file object in destructor. E.g. this
f = open('abc') __del__ f
for sure, closes the file. Python deletes object if its use count reaches zero. E.g.
def g(): f = open('abc') # f is deleted at the end of function and the file is closed g()
If you store result of readlines, the file object does store and it is closed after reading.
a = open('abs').readlines() # here the file is closed becuase its temporal object is holded only until it is used in readlines.
In the construction
sum(1 for i in open('/path/ABC.txt',"r") if i.strip())
for holds the temporal file object which the use count reaches the zero when execution outs of the sum operator.
You do not need additional readlines to close the file.
You can check opened files using psutil
import psutil proc = psutil.Process() print(proc.open_files()
import warnings; warnings.simplefilter('default')- Python will tell you if you missed closing a file.ResourceWarning : unclosed file