2014-02-05 58 views
1

我在Python3.3應用程序中使用XlsxWriter和Excel文件時,打開和運行腳本的py這個錯誤發生了,我可以用除except PermissionError沒有處理,處理異常xlswriter的PermissionError在python3

錯誤:

Exception PermissionError: PermissionError(13, 'Permission denied') in <bound method Workbook.__del__ of <xlsxwriter.workbook.Workbook object at 0x00000000032C3400>> ignored

我該如何處理這個錯誤,試試?

+0

嘗試除異常作爲味精: –

回答

1

您只需在close()附近添加try/except以處理文件已被Excel打開和鎖定的情況。

try: 
    workbook.close() 
except: 
    # Handle your exception here. 
    print("Couldn't create xlsx file") 

如果需要,您可以爲特定條件添加特定異常處理程序。

0
import xlsxwriter 

try: 
    # Create a workbook and add a worksheet. 
    workbook = xlsxwriter.Workbook('Expenses01.xlsx') 
    worksheet = workbook.add_worksheet() 

    # Some data we want to write to the worksheet. 
    expenses = (
     ['Rent', 1000], 
     ['Gas', 100], 
     ['Food', 300], 
     ['Gym', 50], 
    ) 

    # Start from the first cell. Rows and columns are zero indexed. 
    row = 0 
    col = 0 

    # Iterate over the data and write it out row by row. 
    for item, cost in (expenses): 
     worksheet.write(row, col,  item) 
     worksheet.write(row, col + 1, cost) 
     row += 1 

    # Write a total using a formula. 
    worksheet.write(row, 0, 'Total') 
    worksheet.write(row, 1, '=SUM(B1:B4)') 

    workbook.close() 
except: 
    # 
    #DO WHAT YOU WANT TO DO 

試試這個。