[Tutor] fileobject.close() question
Tim Condit
timc@ans.net
Wed, 12 Apr 2000 18:38:37 +0000 (GMT)
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."
...
>>>