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


    Python_报错:

    D:Python36python.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:Python36libsite-packagesopenpyxlworkbookworkbook.py", line 367, in save
    save_workbook(self, filename)
    File "D:Python36libsite-packagesopenpyxlwriterexcel.py", line 282, in save_workbook
    archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
    File "D:Python36libzipfile.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文件是打开的,资源被占用,在进行操作时,关闭写入的文件即可

    运行结果:

  • 相关阅读:
    windows常规
    oracle常规操作
    idea使用
    java-maven
    java-rabbimq
    centos7 rabbitMq 安装教程
    Leetcode 332.重新安排行程
    Leetcode 334.递增的三元子序列
    Leetcode 331.验证二叉树的前序序列化
    Leetcode 330.按要求补齐数组
  • 原文地址:https://www.cnblogs.com/rychh/p/11421317.html
Copyright © 2020-2023  润新知