[Tutor] A further question about opening and closing files

richard kappler richkappler at gmail.com
Wed Sep 9 16:24:57 CEST 2015


Under a different subject line (More Pythonic?) Steven D'Aprano commented:

> And this will repeatedly open the file, append one line, then close it
> again. Almost certainly not what you want -- it's wasteful and
> potentially expensive.

And I get that. It does bring up another question though. When using

with open(somefile, 'r') as f:
    with open(filename, 'a') as f1:
        for line in f:

the file being appended is opened and stays open while the loop iterates,
then the file closes when exiting the loop, yes? Does this not have the
potential to be expensive as well if you are writing a lot of data to the
file?

I did a little experiment:

>>> f1 = open("output/test.log", 'a')
>>> f1.write("this is a test")
>>> f1.write("this is a test")
>>> f1.write('why isn\'t this writing????')
>>> f1.close()

monitoring test.log as I went. Nothing was written to the file until I
closed it, or at least that's the way it appeared to the text editor in
which I had test.log open (gedit). In gedit, when a file changes it tells
you and gives you the option to reload the file. This didn't happen until I
closed the file. So I'm presuming all the writes sat in a buffer in memory
until the file was closed, at which time they were written to the file.

Is that actually how it happens, and if so does that not also have the
potential to cause problems if memory is a concern?

regards, Richard
-- 

All internal models of the world are approximate. ~ Sebastian Thrun


More information about the Tutor mailing list