问题描述:

I have a python script that analyses a file tree and records its findings in an xlsx.

Analysis is going fine, but when I try to record my results, I get an error:

Traceback (most recent call last):

File ".\call_validation.py", line 103, in <module>

wb.save(wb_name)

File "C:\Python\lib\site-packages\openpyxl\workbook\workbook.py", line 298, i

save_workbook(self, filename)

File "C:\Python\lib\site-packages\openpyxl\writer\excel.py", line 196, in sav

writer.save(filename, as_template=as_template)

File "C:\Python\lib\site-packages\openpyxl\writer\excel.py", line 178, in sav

archive = ZipFile(filename, 'w', ZIP_DEFLATED)

File "C:\Python\lib\zipfile.py", line 923, in __init__

self.fp = io.open(file, modeDict[mode])

OSError: [Errno 22] Invalid argument: 'move_generated-2015-05-07 10:08:26.xlsx'

I am generating my filename using datetime.datetime.now() like so:

save_time = str(datetime.datetime.now()).split(".")[0]

wb_name = "move_generated-" + save_time + ".xlsx"

wb.save(wb_name)

I don't believe the filename is too long, its only in C:\code\call_flow and I've tried stripping all the non-alphanumeric characters out of the name. Any ideas?

EDIT: Solution ended up being that I had failed to strip the colons from the time. As @nivix zixer said I fixed it by replacing

save_time = str(datetime.datetime.now()).split(".")[0]

with

save_time = str(datetime.datetime.now()).split(".")[0].replace(':', '_')

网友答案:

Perhaps the problem is you have a space in the filename?

Replace str(datetime.datetime.now()).split(".")[0] with this: str(datetime.datetime.now()).split(".")[0].replace(' ', '_').

Glad I could help Will!

相关阅读:
Top