File handle not being released by close

Eric Brunel see.signature at no.spam
Mon Jul 30 10:48:57 EDT 2007


On Mon, 30 Jul 2007 16:36:00 +0200, <bg_ie at yahoo.com> wrote:

> Hi,
>
> 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?
>
> /Barry
>
> import os, time, string
>
> dir = 'c:\\temp1'
>
> def listAllFile(fileNames,dir,files):
>   def f1(a,dir=dir): return os.path.join(dir,a)
>   files2 = map(f1, files)
>   fileNames.extend(files2)
>
> fileNames = []
> os.path.walk(dir,listAllFile,fileNames)
>
> for fileName in fileNames:
>   fileBeginning = os.path.splitext(fileName)[0]
>   fileEnd       = os.path.splitext(fileName)[1]
>
>   if fileEnd == ".py":
>     print fileName
>     f=open(fileBeginning+".tmp", 'w')
>     f.write("Hello")
>     f.close

f.close()

HTH...
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"



More information about the Python-list mailing list