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?

4
  • 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. Commented Feb 27, 2014 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") Commented Feb 27, 2014 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. Commented Feb 27, 2014 at 13:40
  • @RemcoGerlich ,That's what i also thought... :) :) Commented Feb 27, 2014 at 13:43

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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?.

2 Comments

however, if using with open(...), you still don't need to worry about close().
True, that's why I love to use with open(...). More robust, since you don't have to bother about closing file stream.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.