diff --git a/pandas/_libs/parsers.pyx b/pandas/_libs/parsers.pyx index adc7a650b745f..bb1493280dfd2 100644 --- a/pandas/_libs/parsers.pyx +++ b/pandas/_libs/parsers.pyx @@ -657,7 +657,7 @@ cdef class TextReader: if isinstance(source, str): encoding = sys.getfilesystemencoding() or "utf-8" - + usource = source source = source.encode(encoding) if self.memory_map: @@ -677,10 +677,11 @@ cdef class TextReader: if ptr == NULL: if not os.path.exists(source): + raise FileNotFoundError( ENOENT, - f'File {source} does not exist', - source) + f'File {usource} does not exist', + usource) 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 590f26a76802a..fe360f1346c7c 100644 --- a/pandas/tests/io/parser/test_common.py +++ b/pandas/tests/io/parser/test_common.py @@ -2,7 +2,6 @@ Tests that work on both the Python and C engines but do not have a specific classification into the other test modules. """ - import codecs from collections import OrderedDict import csv @@ -978,15 +977,15 @@ def test_path_local_path(all_parsers): def test_nonexistent_path(all_parsers): # gh-2428: pls no segfault # gh-14086: raise more helpful FileNotFoundError + # GH#29233 "File foo" instead of "File b'foo'" parser = all_parsers path = "{}.csv".format(tm.rands(10)) - msg = "does not exist" if parser.engine == "c" else r"\[Errno 2\]" + msg = f"File {path} does not exist" if parser.engine == "c" else r"\[Errno 2\]" with pytest.raises(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