0

So I have a XLS file that is being accessed via the xlutils library. When my program is finished doing its process, its supposed to delete the original file and rename the temporary file to the original file. The data in the excel file is being input a website and data from the website is being extracted and then written to the excel sheet. All this is fine, however, when its done its having an issue renaming the temp file.

The temp file is simply a copy made via the following function:

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

I have created a function which is called when the for loop for inputting data and extracting is finished:

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)

Using Process Explorer, it shows that the temp file is still open in python.exe. I don't know how to close the file and free memory since it is being opened by using:

rbook=open_workbook(file)
sheet = rbook.sheet_by_index(0)

where file will simply be replaced with whatever self.temp is.

When a GO button is pushed the following happens:

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

So the name of the file which is in a SearchCtrl called "pathBox" is obtained and a temp copy is made and then renamed.

ohbrobig
  • 939
  • 2
  • 13
  • 34
  • Did you try to use self.temp.close()? You should always close files when you are done with them. – Werner May 19 '15 at 11:25
  • Well, self.temp is just a string. Hence: AttributeError: 'unicode' object has no attribute 'close' when trying that statement – ohbrobig May 19 '15 at 18:56
  • sorry thought it was the file object. I think I need to see more code as I can't see where 'file' is coming from which you give to open_workbook. – Werner May 20 '15 at 06:36
  • Posted more code. Does that help? – ohbrobig May 20 '15 at 19:52
  • If 'openFile(file)' is the method which contains the open_workbook call then I don't see where a file object is kept open. I don't think open_workbook is the culprit as it closes the file it opens. Maybe create a self containing script showing the problem and post that so one can run it in a debugger and see. – Werner May 22 '15 at 06:53

0 Answers0