I think this question is more of a “coding style” rather than technical issue.
Said I have a line of code:
buf = open('test.txt','r').readlines()
...
Will the file descriptor automatically close, or will it stay in the memory?
If the file descriptor is not closed, what is the prefer way to close it?
If you assign the file object to a variable, you can explicitly close it using
.close()Alternatively (and more generally preferred), you can use the
withkeyword (Python 2.5 and greater) as mentioned in the Python docs: