0

Good evening SE'ers,

I've got a question that has been bugging me for the last twenty-four hours. I've read up on the issue and my issue seems to be just plain stupid. So, I must be doing something wrong.

FIRST I'm using xlrd, xlwt and xlutils to create an excel doc and reopen it to check, update and write (save) out over it. Something is causing it to not work correctly and it's apparently only when it saves OVER itself with an updated (copy) workbook.

I got a good piece of information from this "ask", but it doesn't apply to me... IOError: [Errno 22] invalid mode ('wb') or filename:

SECOND My issue is that I have this as my error:

IOError: [Errno 22] invalid mode ('w+b') or filename: u'D:/LocalData/[username]/Desktop/test1_.xls'

Note, the user name is actually not [username].

The traceback is listed here:

# Error: 22
# Traceback (most recent call last):
#   File "<maya console>", line 3, in <module>
#   File "D:/LocalData/[username]/Documents/maya/scripts\ExportExcel.py", line 144, in main
#     writeExcel()
#   File "D:/LocalData/[username]/Documents/maya/scripts\ExportExcel.py", line 107, in writeExcel
#     writeInt(wb,wsInt,filePath,detectName,fileName)
#   File "D:/LocalData/[username]/Documents/maya/scripts\ExportExcel.py", line 48, in writeInt
#     logTheMatList(wb,wsInt,filePath,detectName)
#   File "D:/LocalData/[username]/Documents/maya/scripts\ExportExcel.py", line 35, in logTheMatList
#     wb.save(filePath+'MaterialList_'+str(detectName)+'.xls')
#   File "D:\Program Files\Autodesk\Maya2013\Python\lib\xlwt\Workbook.py", line 696, in save
#     doc.save(filename_or_stream, self.get_biff_data())
#   File "D:\Program Files\Autodesk\Maya2013\Python\lib\xlwt\CompoundDoc.py", line 262, in save
#     f = open(file_name_or_filelike_obj, 'w+b')

EDIT1:

Figured out that I cannot open a file that and overwrite it. I'm not sure why, but if it's the same name and file location, it highly disapproves and provides an error. Does anyone have any suggestions as to how to avoid this obvious issue?

4

1 回答 1

0

我自己想出来的。

如果您在使用 os、xlrd、xlwt 和 xlutils 时打开一个 excel 文档以读取和保存...您必须遵循这些说明(可能由比我了解较少的人解决;))。

  1. import所有重要的、必要的模块
  2. rb=xlrd.open_workbook([your location here]+[your file name]+'.xls') 警告不要使用on_demand=True,这会使您的文件保持打开状态
  3. os.remove([your location here]+[your file name]+'.xls')
  4. wb=copy(rb)
  5. wb.save([your location here]+[your file name]+'.xls')

这是在不“删除”文件的情况下更新您创建的文件(如程序中的典型保存)的最有效方法,您将保存一个较新的副本。

于 2015-05-06T18:58:54.433 回答