[Tutor] fileobject.close() question
D-Man
dsh8290@rit.edu
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.
-D
Tim Condit wrote:
>
> Greetings,
>
> 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?
>
> Thanks,
> Tim
>
> 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',
> 'testfile')
> >>> 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
> http://www.python.org/mailman/listinfo/tutor