File handle not being released by close

bg_ie at yahoo.com bg_ie at yahoo.com
Mon Jul 30 16:36:00 CEST 2007


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




More information about the Python-list mailing list