+ 1
Why when i use this method i don't need to close the file
I tried this with open('hw.txt',mode='w')as f: f.write('hello world') And i didn't need to close the file
2 Antworten
+ 5
The with block is called a context manager. The variable f is only valid within the with block. As soon as you leave the block, you're basically telling the interpreter that you're not going to use the file (which is associated with the variable f) anymore. So there is no reason to keep the file open and python closes it automatically for you.
0
Thank you ❤