Python没有发布文件/无法保存文件(xlutils)

时间:2015-05-19 03:15:24

标签: python wxpython xlwt xlutils

所以我有一个通过xlutils库访问的XLS文件。当我的程序完成其进程时,它应该删除原始文件并将临时文件重命名为原始文件。 excel文件中的数据正在输入网站,并且正在提取来自网站的数据,然后将其写入Excel工作表。但是,当它完成重命名临时文件的问题时,这一切都很好。

临时文件只是通过以下函数制作的副本:

def rename(self, fpath):
    tempf=os.path.splitext(fpath)[0]
    tempf=tempf + "-output" + ".xls"
    shutil.copyfile(fpath,tempf)
    return tempf

我创建了一个函数,当用于输入数据和提取的for循环结束时调用该函数:

def allDone(self, event):
    dlg = wx.MessageBox("All done!", "Ask Alfred", wx.OK | wx.ICON_INFORMATION)
    os.unlink(self.fpath)
    os.rename(self.temp, self.fpath)

使用Process Explorer,它显示临时文件仍在python.exe中打开。我不知道如何关闭文件和释放内存,因为它是通过使用:

打开的
rbook=open_workbook(file)
sheet = rbook.sheet_by_index(0)

其中file将简单地替换为self.temp。

按下GO按钮时会发生以下情况:

def onGo(self, event):
    fpath=self.pathBox
    fpath=fpath.GetValue()
    self.fpath=fpath
    temp=myClass.rename(fpath)
    self.temp=temp
    openFile(temp)

def rename(self, fpath):
    tempf=os.path.splitext(fpath)[0]
    tempf=tempf + ".alf"
    shutil.copyfile(fpath,tempf)
    return tempf

因此,SearchCtrl中的文件名称为" pathBox"获取并生成临时副本,然后重命名。

0 个答案:

没有答案