You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Jupyter Notebook中使用pandas读取文件遇FileNotFoundError[Errno2]

解决Jupyter Notebook读取Excel文件的FileNotFoundError及编码问题

一、解决FileNotFoundError(文件找不到错误)

1. 修正路径转义问题

Python中\是转义字符,直接写raw data\Students.xlsx会被解析为raw data\\Students.xlsx,可能与实际路径不匹配。三种解决方式任选其一:

  • 使用原始字符串(加r前缀):
    df_weather = pd.read_excel(r'raw data\Students.xlsx')
    
  • 改用正斜杠/:
    df_weather = pd.read_excel('raw data/Students.xlsx')
    
  • 用双反斜杠\\:
    df_weather = pd.read_excel('raw data\\Students.xlsx')
    

2. 确认文件位置与工作目录匹配

先运行以下代码查看当前Jupyter的工作目录:

import os
print(os.getcwd())
  • 如果raw data文件夹不在该目录下,要么将文件夹移动到当前工作目录,要么使用绝对路径读取文件(示例为Windows路径,加r前缀避免转义):
    df_weather = pd.read_excel(r'C:\Users\你的用户名\Desktop\raw data\Students.xlsx')
    
  • 更稳妥的方式是用os.path.join拼接路径,避免手动输入错误:
    data_folder = os.path.join(os.getcwd(), 'raw data')
    file_path = os.path.join(data_folder, 'Students.xlsx')
    df_weather = pd.read_excel(file_path)
    

3. 检查文件名/文件夹名的拼写细节

  • 注意大小写:Mac/Linux系统区分文件名大小写,比如Students.xlsx和students.xlsx是两个不同文件;
  • 确认没有多余空格或特殊字符,比如文件夹名是否为raw_data而非raw data。

二、关于Excel编码提示问题

Excel文件本身并非UTF-8文本格式,read_excel会自动处理Excel的内置编码,你遇到的"非Unicode-8编码"提示大概率是用文本编辑器打开Excel文件导致的,不影响pd.read_excel读取。若真出现编码相关报错,可指定engine参数(需先安装openpyxl:pip install openpyxl):

df_weather = pd.read_excel(file_path, engine='openpyxl')

内容的提问来源于stack exchange,提问作者Zachery Wilson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 05:25:27