File handle not being released by close

bg_ie at bg_ie at
Mon Jul 30 16:36:00 CEST 2007


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?


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 = []

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')

More information about the Python-list mailing list