File handle not being released by close
Peter Otten
__peter__ at web.de
Mon Jul 30 10:48:50 EDT 2007
bg_ie at yahoo.com wrote:
> I'm in the process of writing some code and noticed a strange problem
> while doing so. I'm working with PythonWin 210 built for Python 2.5. I
> noticed the problem for the last py file processed by this script,
> where the concerned tmp file is only actually written to when
> PythonWin is closed. In other words, after I run this script, one of
> the generated tmp files has a size of 0kB. I then close PythonWin and
> it is then written to.
>
> I'm guessing the garbage collector is causing the file to be written,
> but shouldn't close do this?
Yes.
> f=open(fileBeginning+".tmp", 'w')
> f.write("Hello")
> f.close
In Python () is nessary to call a parameterless function or method:
>>> def f(): print "Hi Barry"
...
>>> f
<function f at 0xb7cf2374>
>>> f()
Hi Barry
This allows it to treat functions as variables consistently:
>>> def f(write):
... write("Hello")
...
>>> def write_upper(s): print s.upper()
...
>>> import sys
>>> write_to_stdout = sys.stdout.write
>>>
>>> f(write_upper)
HELLO
>>> f(write_to_stdout)
Hello>>>
Peter
More information about the Python-list
mailing list