Python_报错:PermissionError: [Errno 13] Permission denied: 'XXXXX.xlsx'

谁说我不能喝 提交于 2020-12-25 02:53:15

Python_报错:

D:\Python36\python.exe D:/test_excel/excel_001.py
Traceback (most recent call last):
File "D:/test_excel/excel_001.py", line 14, in <module>
wb.save("e:\\sample.xlsx")
File "D:\Python36\lib\site-packages\openpyxl\workbook\workbook.py", line 367, in save
save_workbook(self, filename)
File "D:\Python36\lib\site-packages\openpyxl\writer\excel.py", line 282, in save_workbook
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
File "D:\Python36\lib\zipfile.py", line 1090, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'e:\\sample.xlsx'

源码:

import locale
from openpyxl import Workbook
import datetime
import time
import locale
wb = Workbook()
ws = wb.active
ws['A2']= datetime.datetime.now()#写入一个当前时间
#写入一个自定义的时间格式
locale.setlocale(locale.LC_CTYPE,'chinese')
ws['A3'] = time.strftime("%Y年%m月%d日 %H时%M分%S秒",time.localtime())

# Save the file
wb.save("e:\\sample.xlsx")

原因:excel文件是打开的,资源被占用,在进行操作时,关闭写入的文件即可

运行结果:

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!