[Tutor] fileobject.close() question
Wed, 12 Apr 2000 15:21:47 -0400
I remember something about buffers. I think that when you do a
file.write() it only writes it to the file object in memory. When you
do a file.close() it dumps the memory buffer to the actual file on the
disk. I think there was another way to dump the buffer to the disk
without closeing the file, but I don't remember now what it is.
Tim Condit wrote:
> I'm wondering about something Pythonic. The first time I ran the little
> code snip below, I left the 'fileobject.close()' function out. Basically,
> I forgot it, but at the same time, in the Quick Python book, pg. 132,
> section 13.2, curiously enough, it says:
> "In small scripts, not closing a file object will generally
> not have much of an effect.."
> I'm seeing something different. Before I caught my oversight, I ran this
> same little snip twice.. the first time to create the file, and the second
> time to trigger the else: statement. The file was created, but nothing was
> written to it, until I went back and added fileobject.close(). Does anyone
> know what is causing this to happen?
> FYI: the third line (testfile = ...) is all on one line)
> >>> dir()
> ['__builtins__', '__doc__', '__name__']
> >>> import os
> >>> testfile = os.path.join('/', 'afs', 'ans.net', 'user', 'timc',
> >>> if not os.path.isfile(testfile):
> ... fileobject = open(testfile, 'w')
> ... fileobject.write("hi there.")
> ... fileobject.close()
> ... else:
> ... print "Sorry, that file already exists."
> Tutor maillist - Tutor@python.org