From b124c388e4f3e0a8d88bb3025d8c1aee7ab3e4e1 Mon Sep 17 00:00:00 2001 From: Matt Williams Date: Thu, 13 Dec 2018 12:59:20 +0000 Subject: [PATCH] EHN: Add filename to FileNotFoundError --- pandas/_libs/parsers.pyx | 5 ++++- pandas/tests/io/parser/test_common.py | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/parsers.pyx b/pandas/_libs/parsers.pyx index 0b7edaf04a1ed..e0fcf102701f4 100644 --- a/pandas/_libs/parsers.pyx +++ b/pandas/_libs/parsers.pyx @@ -6,6 +6,7 @@ import time import warnings from csv import QUOTE_MINIMAL, QUOTE_NONNUMERIC, QUOTE_NONE +from errno import ENOENT from libc.stdlib cimport free from libc.string cimport strncpy, strlen, strcasecmp @@ -696,7 +697,9 @@ cdef class TextReader: if ptr == NULL: if not os.path.exists(source): raise compat.FileNotFoundError( - 'File {source} does not exist'.format(source=source)) + ENOENT, + 'File {source} does not exist'.format(source=source), + source) raise IOError('Initializing from file failed') self.parser.source = ptr diff --git a/pandas/tests/io/parser/test_common.py b/pandas/tests/io/parser/test_common.py index b1547181350bc..6860452f5ccc4 100644 --- a/pandas/tests/io/parser/test_common.py +++ b/pandas/tests/io/parser/test_common.py @@ -898,9 +898,15 @@ def test_nonexistent_path(all_parsers): msg = ("does not exist" if parser.engine == "c" else r"\[Errno 2\]") - with pytest.raises(compat.FileNotFoundError, match=msg): + with pytest.raises(compat.FileNotFoundError, match=msg) as e: parser.read_csv(path) + filename = e.value.filename + filename = filename.decode() if isinstance( + filename, bytes) else filename + + assert path == filename + def test_missing_trailing_delimiters(all_parsers): parser = all_parsers