[Tutor] fileobject.close() question

Wed, 12 Apr 2000 15:56:15 -0400

     This has to do with buffer flushing, to be honest I'm not sure how to set 
     autoflushing with python (perl would be $| = 1;), but anyway, the buffer 
     waits to be filled before it writes and it flushes on a close.  If you 
     don't write enough to fill the buffer, and the program exits, the buffer is 
     cleaned up before the close occurs, therefore the remaining bytes are lost. 
      Not closing a file handle is not a big deal with reads (at all), but I'd 
     be careful with writes.
______________________________ Reply Separator _________________________________
Subject: [Tutor] fileobject.close() question 
Author:  Tim Condit <timc@ans.net> at smtplink
Date:    4/12/00 6:38 PM

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