Errno 22尝试将数据帧导出到excel时参数无效



我正在尝试将数据帧导出到excel(在Windows中(。我的代码如下:

import os
import pandas as pd
clients_file = pd.read_excel("count.xlsx", engine="openpyxl", header=0)
clients_file.to_excel(os.path.join("path", "backup", "weekly", "12345", "12345" + "_" + pd.datetime.today().strftime('%Y-%m-%d-%H:%M:%S')) + ".xlsx", index=False, sheet_name="report") 

我的问题是我得到了这个错误:

OSError: [Errno 22] Invalid argument: 'path\backup\weekly\12345\12345_2022-03-03-12:10:56.xlsx'

我真的不知道为什么。我知道我可以使用"+"连接字符串,但path.join更适合阅读。有什么想法吗?干杯

如果在Windows中执行此操作,则不允许将:作为文件名的一部分。建议:

date_str = pd.datetime.today().strftime('%Y-%m-%d-%H-%M-%S'))
clients_file.to_excel(os.path.join("path", 
"backup", 
"weekly", 
"12345", 
"12345_" + date_str + ".xlsx", 
index=False, 
sheet_name="report")

最新更新