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

Jupyter Notebook中pandas读取CSV遇FileNotFoundError求助

读取CSV文件时的FileNotFoundError排查

执行的代码

pwd
ls
import pandas as pd
DF = pd.read_csv('~/downloads/world_mortality.csv')

报错信息

FileNotFoundError                         Traceback (most recent call last)
Input In [10], in <cell line: 1>()
----> 1 DF = pd.read_csv('~/downloads/world_mortality.csv')

File ~\anaconda3\lib\site-packages\pandas\util\_decorators.py:311, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs)
    305 if len(args) > num_allow_args:
    306     warnings.warn(
    307         msg.format(arguments=arguments),
    308         FutureWarning,
    309         stacklevel=stacklevel,
    310     )
--> 311 return func(*args, **kwargs)

File ~\anaconda3\lib\site-packages\pandas\io\parsers\readers.py:678, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options)
    663 kwds_defaults = _refine_defaults_read(
    664     dialect,
    665     delimiter,
   (...)
    674     defaults={"delimiter": ","},
    675 )
    676 kwds.update(kwds_defaults)
--> 678 return _read(filepath_or_buffer, kwds)

File ~\anaconda3\lib\site-packages\pandas\io\parsers\readers.py:575, in _read(filepath_or_buffer, kwds)
    572 _validate_names(kwds.get("names", None))
    574 # Create the parser.
--> 575 parser = TextFileReader(filepath_or_buffer, **kwds)
    577 if chunksize or iterator:
    578     return parser

File ~\anaconda3\lib\site-packages\pandas\io\parsers\readers.py:932, in TextFileReader.__init__(self, f, engine, **kwds)
    929     self.options["has_index_names"] = kwds["has_index_names"]
    931 self.handles: IOHandles | None = None
--> 932 self._engine = self._make_engine(f, self.engine)

File ~\anaconda3\lib\site-packages\pandas\io\parsers\readers.py:1216, in TextFileReader._make_engine(self, f, engine)
   1212     mode = "rb"
   1213 # error: No overload variant of "get_handle" matches argument types
   1214 # "Union[str, PathLike[str], ReadCsvBuffer[bytes], ReadCsvBuffer[str]]"
   1215 # , "str", "bool", "Any", "Any", "Any", "Any", "Any"
-> 1216 self.handles = get_handle(  # type: ignore[call-overload]
   1217     f,
   1218     mode,
   1219     encoding=self.options.get("encoding", None),
   1220     compression=self.options.get("compression", None),
   1221     memory_map=self.options.get("memory_map", False),
   1222     is_text=is_text,
   1223     errors=self.options.get("encoding_errors", "strict"),
   1224     storage_options=self.options.get("storage_options", None),
   1225 )
   1226 assert self.handles is not None
   1227 f = self.handles.handle

File ~\anaconda3\lib\site-packages\pandas\io\common.py:786, in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
    781 elif isinstance(handle, str):
    782     # Check whether the filename is to be opened in binary mode.
    783     # Binary mode does not support 'encoding' and 'newline'.
    784     if ioargs.encoding and "b" not in ioargs.mode:
    785         # Encoding
--> 786         handle = open(
    787             handle,
    788             ioargs.mode,
    789             encoding=ioargs.encoding,
    790             errors=errors,
    791             newline="",
    792         )
    793     else:
    794         # Binary mode
    795         handle = open(handle, ioargs.mode)

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\user/downloads/world_mortality.csv'

问题描述

在Jupyter Notebook中尝试读取Downloads文件夹内的world_mortality.csv文件时触发FileNotFoundError,多次尝试解决未成功,需排查问题原因。

排查步骤

  • 修正路径格式:从错误路径能看到混合了Windows反斜杠和Linux正斜杠,且~在Windows Python环境中可能无法正确解析为用户目录。建议替换为完整绝对路径,比如C:\\Users\\user\\Downloads\\world_mortality.csv,或用原始字符串避免转义问题:r'C:\Users\user\Downloads\world_mortality.csv'
  • 确认文件真实存在:
    1. 打开文件资源管理器进入Downloads文件夹,检查world_mortality.csv是否存在
    2. 右键文件选择「属性」,复制完整路径直接粘贴到代码中
  • 检查文件名细节:Windows默认Downloads文件夹名首字母大写,确认代码中是否写成了小写的downloads;同时检查文件名拼写,比如后缀是否为.csv而非其他格式
  • 用Jupyter命令验证:执行!dir(Windows)查看当前工作目录的文件;执行!dir C:\Users\user\Downloads确认目标文件夹内的文件列表

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 10:35:34