In other words, when would Python close the file in first example? Would it be done immediately after reading from a file and realizing than there's no references to the file handler returned by open('file.txt')
? If that's the case, it seems that with
statement is not necessary here. Are there any caveats?
Asked
Active
Viewed 27 times
0

krvkir
- 771
- 7
- 12
-
1Does this answer your question? https://stackoverflow.com/questions/8011797/open-read-and-close-a-file-in-1-line-of-code/8011836 – Mitchell Olislagers Feb 05 '21 at 12:08
-
Well, I hoped to find out what exactly happens in both cases. But that answer is the best approach to that. Thanks! – krvkir Feb 05 '21 at 12:53