1

On OS X, openpyxl.save() is working fine for a file called 'all_done.xslx'.

Yet when it is attempted on Windows, it results in:

c:\Users\Tony\Desktop\ROI>python roi_cut6.py > log.txt
Traceback (most recent call last):
File "roi_cut6.py", line 373, in <module>
main()
File "roi_cut6.py", line 369, in main
processSource(wb, 'Gemini', totalGeminiSpends, geminiRevenues)
File "roi_cut6.py", line 269, in processSource
wb.save(r'all_done.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: 'all_done.xlsx'

This post and this post suggest adding r before the filename. This I've already tried (as can be seen in the log above). And the file is in the current directory, so it's not an issue with the backslashes or forward slashes. The file is openable in Excel on both OS X and Windows. What else could be the problem? Unfortunately I don't have access to the Windows mahcine myself so cannot troubleshoot directly there.

Does it make any difference whether the file being saved to was originally created directly in Excel or rather with openpyxl?

Community
  • 1
  • 1
Pyderman
  • 14,809
  • 13
  • 61
  • 106

1 Answers1

3

This is probably a permissions error and nothing to do with openpyxl. The idea of using r is totally wrong. Use os.path to compose a path where whatever user is running the script has the permission to write.

Charlie Clark
  • 18,477
  • 4
  • 49
  • 55
  • The idea of using `r` is useful if you have a windows path name using backslashes like `c:\path\to\file.txt`. But in this case using a raw string doesn't make a difference, since your filepath is only a filename with no special characters. – Håken Lid Jul 12 '15 at 12:48
  • @HåkenLid. It really is not a good idea to get used to using r'' strings for anything other than regexes. Always use `os.path` for files. This makes code more portable and more reliable. – Charlie Clark Jul 12 '15 at 17:03
  • A permission error (`EACCES`) should be identified as such. That `errno` is set to `EINVAL` implies either an invalid argument or that some unmapped Windows error has occurred. Inspecting this kind of problem is easiest with a platform debugger such as WinDbg or cdb, to get the underlying Windows error code and kernel status code. – Eryk Sun Jul 12 '15 at 19:58