为了账号安全,请及时绑定邮箱和手机立即绑定

尝试使用 save() 保存已编辑的工作簿时出现“OSError: [Errno 22]

尝试使用 save() 保存已编辑的工作簿时出现“OSError: [Errno 22]

慕码人2483693 2023-12-20 10:29:27
我的 .xls 文件与 .py 文件位于同一目录中。尽管如果我不覆盖现有文件,它工作得很好。import xlrd, xlwtfrom xlutils.copy import copyrb2=xlrd.open_workbook("TEST_info.xls",on_demand=True)wb2=copy(rb2)ws2=wb2.get_sheet(0)ws2.write(0,1,'DIFFERENt value')wb2.save("TEST_info.xls")文件“C:\Python_User\Excel_spreadsheet_proj\excel_proj_env\Scripts\main.py”,第 9 行,在 wb2.save(“TEST_info.xls”) 文件“C:\Python_User\Excel_spreadsheet_proj\excel_proj_env\lib\site-packages\xlwt \Workbook.py”,第 710 行,保存 doc.save(filename_or_stream, self.get_biff_data()) 文件“C:\Python_User\Excel_spreadsheet_proj\excel_proj_env\lib\site-packages\xlwt\CompoundDoc.py”,第 262 行,在 save f = open(file_name_or_filelike_obj, 'w+b') OSError: [Errno 22] 无效参数: 'TEST_info.xls'
查看完整描述

2 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

根据https://xlrd.readthedocs.io/en/latest/on_demand.html上的文档:

on_demand=Trueand BIFF version >= 5.0open_workbook()加载全局数据并返回,不释放资源。

您应该设置on_demand=TrueFalse释放rb2底层资源,或者rb2.release_resources()在尝试保存副本之前调用。


查看完整回答
反对 回复 2023-12-20
?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

我最终导入“os”并编写:

if(os.path.exists(file)):
    os.remove(file)
wb.save(file)



查看完整回答
反对 回复 2023-12-20
  • 2 回答
  • 0 关注
  • 50 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信