我在工作中将 Python 与 pandas 和 xlsxwriter 一起用于自动化项目,但我需要将其作为服务运行,为此我使用 pyinstaller 构建可执行文件并使用 nssm.exe 安装服务。当我在安装服务之前使用 pyinstaller 并执行 .exe 文件时,会生成成功的输出文件。但是在服务安装并启动后,返回以下错误。
2019-05-16 10:10:30,268 -- GENERIC EXCEPTION: Traceback (most recent call last):
File "service.py", line 75, in main
File "bot.py", line 87, in inicia_consulta
File "bot.py", line 26, in salva_saida
File "site-packages\pandas\io\excel.py", line 1952, in save
File "site-packages\xlsxwriter\workbook.py", line 304, in close
File "site-packages\xlsxwriter\workbook.py", line 652, in _store_workbook
File "zipfile.py", line 1204, in __init__
FileNotFoundError: [Errno 2] No such file or directory: 'H:/DOC/Afogados/Documentos/BotDetran/16-05-2019_1010_PE.xlsx'
我尝试了各种文件路径功能,但对我有用,还记得该功能在没有服务模式的情况下正常工作。
这是我的功能,我在下面详细说明。
def salva_saida(pd, veiculos):
df = pd.DataFrame(veiculos) # generate dataframe
# capture filedir for the config.yml file
# filedir = 'H:/DOC/Afogados/Documentos/BotDetran/'
filedir = cfg['filedir']
# file = '16-05-2019_1010_PE.xlsx'
file = datetime.now().strftime("%d-%m-%Y_%H%M")+'_PE.xlsx'
# the error ir occurring here
writer = pd.ExcelWriter(filedir+file, engine='xlsxwriter')
...