1

I cannot open a file on Jupyter Notebook on Windows.

I tried all the advices in several posts on StackOverFlow (including this very complete one: Windows path in python)

My code is this one:

today_date = date.today()
path2file = 'D:/Users/XXX123/My_folder/Project/Sub_folder/Relevés/audit/Forms_'+'{:%Y%m%d}'.format(today_date)+'.csv'
print(path2file)
pd.read_csv(os.path.normpath(path2file))

and I got this error message:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-14-01089ab15706> in <module>()
      4 print(path2file)
      5 
----> 6 pd.read_csv(os.path.normpath(path2file))

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
    653                     skip_blank_lines=skip_blank_lines)
    654 
--> 655         return _read(filepath_or_buffer, kwds)
    656 
    657     parser_f.__name__ = name

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
    403 
    404     # Create the parser.
--> 405     parser = TextFileReader(filepath_or_buffer, **kwds)
    406 
    407     if chunksize or iterator:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, engine, **kwds)
    762             self.options['has_index_names'] = kwds['has_index_names']
    763 
--> 764         self._make_engine(self.engine)
    765 
    766     def close(self):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine)
    983     def _make_engine(self, engine='c'):
    984         if engine == 'c':
--> 985             self._engine = CParserWrapper(self.f, **self.options)
    986         else:
    987             if engine == 'python':

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, src, **kwds)
   1603         kwds['allow_leading_cols'] = self.index_col is not False
   1604 
-> 1605         self._reader = parsers.TextReader(src, **kwds)
   1606 
   1607         # XXX

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__ (pandas\_libs\parsers.c:4209)()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source (pandas\_libs\parsers.c:8873)()

FileNotFoundError: File b'D:\\Users\\XXX123\\My_folder\\Project\\Sub_folder\\Relev\xc3\xa9s\\audit\\Forms20180123.csv' does not exist

I tried several solutions: slashes, backslashes, double backslashes, triple backslashes, double or triple quotes, r'...', os.path.normpath('...'),... but I still cannot open my file.

Does anyone has an idea ?

NB: My file does exist...

Thanks for your help.

Yvanou
  • 93
  • 1
  • 3
  • 7
  • provide actual file path – Sociopath Jan 23 '18 at 12:24
  • It said `FileNotFoundError`. So problem is whit accessing the file. If your are sure that the file really exists - try to rename `Relevés` to `Releves`, and retry. Maybe problem is with encoding of that letter. And one more point - Python is case sensitive. – Michał Zaborowski Jan 23 '18 at 12:25
  • A quick way to debug this would be to copy the text from the path itself (right click the file > properties > security tab) and do an equivalency check `path = path2file` where `path` is the string itself. You can use `r` in front of the string to force it into a string-literal, but it would be more advisable to just replace the slashes with backslashes. A string literal can have strange side effects if you don't know what you are doing. – Brandon Barney Jan 23 '18 at 12:55
  • Also, just throwing this out there, there's no need to put your format function in the middle of the string concatenation. You can leave those brackets there, move the format to the end, and keep it as one string. Like this: `'D:/Users/XXX123/My_folder/Project/Sub_folder/Relevés/audit/Forms_{:%Y%m%d}.csv'`.format(today_date)` – Brandon Barney Jan 23 '18 at 12:56
  • Apparently you're using a C extension implemented in parsers.c that's encoding the `str` path as UTF-8 bytes, and then probably calling a low-level C or Windows API to open this bytes path. The C runtime and Windows API decode bytes paths as ANSI. There's no support for UTF-8 at the API level in Windows. It could be that the C extension is confused. Starting with 3.6, `sys.getfilesystemencoding()` is UTF-8 in Windows. But this is only for high-level Python APIs. Internally bytes paths get decoded to the native UTF-16LE encoding. – Eryk Sun Jan 23 '18 at 14:53

2 Answers2

2

try:

import datetime
import os
import pandas as pd
import sys

today_date = datetime.date.today()
path2file = r'D:/Users/XXX123/My_folder/Project/Sub_folder/Relevés/audit/Forms_'+'{:%Y%m%d}'.format(today_date)+'.csv'
# check that the file path exists
if not os.path.exists(path2file):
    print('{} not found'.format(path2file))
    sys.exit()
pd.read_csv(os.path.normpath(path2file))

os.path.exists checks that you have a valid filepath.

Putting the 'r' symbol before your filepath ensures that the string is not changed by the interpreter thinking that one of the backslashes denotes a character code.

Oppy
  • 2,662
  • 16
  • 22
2

Make sure that the folder your file is located can be accessed without any additional privileges.
If you still get the eror, just change the working folder to desktop i.e. move the project folder to desktop and load it from there. This worked for me as the default startup location of jupyter was giving me the same error.

Mayur
  • 21
  • 2