8

Opening & closing file using file object:

fp=open("ram.txt","w")
fp.close()

If we want to Open & close file without using file object ,i.e;

open("ram.txt","w")

Do we need to write close("poem.txt") or writing close() is fine?

None of them are giving any error...

By only writing close() ,How it would understand to what file we are referencing?

user3223301
  • 201
  • 4
  • 11
  • the file will be opened and subsequently closed if you do not assign the output of `open` to a variable. You can't close it and you can't write to it since you don't have a file variable to refer to. – JoshG79 Feb 27 '14 at 13:34
  • @JoshG79,Wrong ,we can still read and write without file object as open("ram.txt","w").readlines() open("ram.txt","w").writelines("YOYO") – user3223301 Feb 27 '14 at 13:37
  • 2
    @user3223301: the file object that's returned by open() will exist as long as it is needed to evaluate the entire expression, and then it will be automatically garbage collected (and closed) because there are no references to it anymore. – RemcoGerlich Feb 27 '14 at 13:40
  • @RemcoGerlich ,That's what i also thought... :) :) – user3223301 Feb 27 '14 at 13:43

3 Answers3

13

For every object in memory, Python keeps a reference count. As long as there are no more references to an object around, it will be garbage collected.

The open() function returns a file object.

f = open("myfile.txt", "w")

And in the line above, you keep a reference to the object around in the variable f, and therefore the file object keeps existing. If you do

del f

Then the file object has no references anymore, and will be cleaned up. It'll be closed in the process, but that can take a little while which is why it's better to use the with construct.

However, if you just do:

open("myfile.txt")

Then the file object is created and immediately discarded again, because there are no references to it. It's gone, and closed. You can't close it anymore, because you can't say what exactly you want to close.

open("myfile.txt", "r").readlines()

To evaluate this whole expression, first open is called, which returns a file object, and then the method readlines is called on that. Then the result of that is returned. As there are now no references to the file object, it is immediately discarded again.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
4

I would use with open(...), if I understand the question correctly. This answer might help you What is the python keyword "with" used for?.

Community
  • 1
  • 1
abuteau
  • 6,963
  • 4
  • 16
  • 20
  • however, if using `with open(...)`, you still don't need to worry about `close()`. – isedev Feb 27 '14 at 13:37
  • True, that's why I love to use with open(...). More robust, since you don't have to bother about closing file stream. – abuteau Feb 27 '14 at 13:38
2

In answer to your actual question... a file object (what you get back when you call open) has the reference to the file in it. So when you do something like:

fp = open(myfile, 'w')
fp.write(...)
fp.close()

Everything in the above, including both write and close, know they reference myfile because that's the file that fp is associated with. I'm not sure what fp.close(myfile) actually does, but it certainly doesn't need the filename after it's open.

Better constructions like

with open(myfile,'w') as fp:
    fp.write(...)

don't change this; in this case, fp is also a context manager, but still contains the pointer to myfile; there's no need to remind it.

Corley Brigman
  • 11,633
  • 5
  • 33
  • 40