In Python you need to give access to a file by opening it.
open() in usual case
If you don’t use with statement, you should write like this:
1 | file = open("welcome.txt") |
file.close() is important, especially in the writing mode. If you don’t close(), the writing action can’t really take effect.
use with in Python IO
1 | with open("welcome.txt") as file: # Use file to refer to the file object |
Notice, that we didn’t have to write file.close() in the case of using with open() as xx. That will automatically be called.
Reference
- With statement in Python : https://www.pythonforbeginners.com/files/with-statement-in-python