Python打开文件时报错:FileNotFoundError: [Errno 2] No such file or directory 的解决办法

微生学
2023-12-01

源代码

import xlrd
wb = xlrd.open_workbook(firewall_file)

 

报错:

FileNotFoundError: [Errno 2] No such file or directory: 'apps/data\\input/xxxx.xlsx'

 

解决办法:

import os

filename = "xxxx.xlsx"

#1、获取当前文件的路径
basedir = os.path.dirname(__file__)

#2、拼接文件路径
update_path = os.path.join(basedir, "data/input", filename)

 

 类似资料: