A fun python CLI program for all to enjoy!

alister alister.ware at ntlworld.com
Sat May 7 04:51:50 EDT 2016


On Sat, 07 May 2016 18:24:45 +1200, Gregory Ewing wrote:

> DFS wrote:
>> Maybe it worked because the last time the file was written to was in a
>> for loop, so I got lucky and the files weren't truncated?  Don't know.
> 
> It "works" because CPython disposes of objects as soon as they are not
> referenced anywhere. Other implementations of Python (e.g. Jython, PyPy)
> might not do that.

to provide an example try the following code in the interactive 
interpreter

>>>f=open('somefile','w')
>>print f.write('line 1')
None
>>>print f.close
built-in method close of file object at 0x7fb4c9580660>
>>>print f.write('line 2')
None
>>>print f.close()
None
>>>print f.write('line 3')
ValueError: I/O operation on closed file

somefile will contain

line 1
line 2





-- 
manager in the cable duct



More information about the Python-list mailing list