In the line:
- open("file","a").write("\n")
an object is created, but where does it go afterwards, and is the file "closed" like in:
-
f = open('file', 'a')
-
f.close()
-
Is there a problem in not closing files?
Although it is not necessary to explicitly close a file, it is good practice to do so.
The file will (if all goes well) be closed when the the object is garbage collected.
If you open a file without keeping a reference to it, there is still an object created which is immediately marked for garbage collection. I cringe when I see people doing this.
If you do keep a reference as in
then the file will remain open as long as that reference exists.
Best practice: call close() as soon as you are done with the file.