← Back to team overview

sikuli-driver team mailing list archive

[Question #673348]: [1.1.3]: Error attempting to delete file

 

New question #673348 on Sikuli:
https://answers.launchpad.net/sikuli/+question/673348

OS: Windows 10

This is more of a Jython question that pure Sikuli, but I have not had success in finding answers elsewhere.  Hoping that you guys here may be able to help!

My script is attempting to do the following:

1.) Download an Excel workbook (XLSX) 
2.) Move the file from the WIndows "downloads" folder to a shared folder
3.) Split the individual worksheets from the original workbook into their own workbooks
4.) Delete the original XLSX file

The error that I am getting is at step #4. It is an error type of "OSError" that says this:

"unlink(): an unknown error occurred: C:\screenshots\SS Case Cancellation Monthly Report.xlsx"

Note that the worksheet splitting is successful. The only failure is the file deletion.

I am using the xlrd library to manipulate the Excel files.  The error always occurs on the "os.remove" line.

Here is the portion of my function that is handling the worksheet splits and file deletion:

    def SplitWorksheets(self):
        import xlrd
        from xlutils.copy import copy

        print "Attempting to split worksheets..."
        
        try:
            x = 0
            base = os.path.splitext(os.path.basename(self.NewFile))[0]
            
            targetdir = (self.FullPath + "\\" + base) #where you want your new files

            #print base
            
            if not os.path.exists(targetdir): #makes your new directory
                os.makedirs(targetdir)
            
            ws_to_split = self.NewFile
            
            wb = xlrd.open_workbook(ws_to_split, on_demand=True)
            
            for sheet in wb.sheets(): #cycles through each sheet in the workbook
                newwb = copy(wb) #makes a temp copy of that book
                newwb._Workbook__worksheets = [ worksheet for worksheet in newwb._Workbook__worksheets if worksheet.name == sheet.name ] #keeps only current sheet
                newwb.save(targetdir + "\\" + sheet.name + ".xls") #Saves single sheet as a new wb with its sheet name
                x += 1

            #Closes the workbook, allowing it to be deleted.
            wb.release_resources()

            wb = None
            
            print "Preparing to delete file: " + self.NewFile
            #Delete original file.
            os.remove(self.NewFile)

            print "Workbook deleted -> " + self.NewFile

            self.NewFile = ""

        except:
            exc_type, exc_val, exc_tb = sys.exc_info()
        
            ErrorText = "***** ERROR IN SCRIPTNAME = " + sys.argv[0] + " *****\n"
            ErrorText += "Date/Time  : " + str(datetime.datetime.now()) + "\n"
            ErrorText += "Host name  : " + socket.getfqdn() + "\n"
            ErrorText += "User name  : " + getpass.getuser() + "\n"
            ErrorText += "Line Number: " + str(exc_tb.tb_lineno) + "\n"
            ErrorText += "Error Type : " + exc_type.__name__ + "\n"
            ErrorText += "Error Value: " + str(exc_val.message) + "\n"
            ErrorText += "***************************************************************\n\r"
        
            print ErrorText

            self.LogError()

        else:
            dpmsg = "=*=*=*=*=* " + str(x) + " Worksheets Split from Workbook *=*=*=*=*="

            print dpmsg
            
        finally:
            sys.exc_clear() 

##########################

Any help is greatly appreciated!

Best regards,

Ron Turrentine

-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.