Python + open() + write on Windows - permission issue (IOError) for file created on OS X

随声附和 提交于 2019-12-25 05:36:08

问题


(I asked this previously, but have since accepted that it is not an issue with openpyxl, so changing tack)

Given an xlsx created on OS X, I open it for writing on that platform using openpyxl load_workbook(). I add some data, then I save it using Workbook.save() (to the same file).

All good on OS X, but when the program, and the XLSX, are transferred onto a Windows machine and executed, I get:

c:\Users\Me\Desktop\ROI>python roi_cut7.py > log.txt
Traceback (most recent call last):
File "roi_cut7.py", line 379, in <module>
main()
File "roi_cut7.py", line 374, in main
processSource(wb, 'Taboola', taboolaSpends, taboolaRevenues)
File "roi_cut7.py", line 276, in processSource
wb.save(r'output.xlsx')
File "C:\Python27\lib\site-packages\openpyxl\workbook\workbook.py", line 298,
in save
save_workbook(self, filename)
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 198, in sa
ve_workbook
writer.save(filename, as_template=as_template)
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 180, in sa
ve
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
File "C:\Python27\lib\zipfile.py", line 756, in __init__
self.fp = open(file, modeDict[mode])
IOError: [Errno 22] invalid mode ('wb') or filename: 'output.xlsx'

Following extensive investigation, I've tried the following:

  1. chmod 777 on the XLSX before moving it to Windows icacls output.xlsx
  2. Ruling out path issues by using os.join and r'' raw notation
  3. icacls output.xlsx /grant everyone:F on the file after moving it to Windows
  4. Unticking the read-only checkbox under Attributes in the Windows folder Properties

... with no success. The Windows folder is a regular Windows folder. I can write to it both manually with new files, and programmatically. It's just the writing to this XLSX that is the issue.

I could update the script so that it reads from one file and writes to another (newly-created by openpyxl) file, but this workaround will only serve for a single day, since each subsequent day, the program needs to build upon the written file from the previous day, and if the file being read from is unchanged, this breaks down. Interestingly, I notice that if I open the XLSX in Excel, save it as a new file, update my program to read and write to this file new file, and re-execute, it works .... but only once. Every subsequent execution results in the IOError.

What would your next step be?

来源:https://stackoverflow.com/questions/31447975/python-open-write-on-windows-permission-issue-ioerror-for-file-created

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