0

I just bumped with and error that I never had the code is the following:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
data=pd.read_csv('Users\ergar\Desktop\ML-mini_bootcamp\W1')

I confirmed several times the path and it is correct. the error message is the following

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-52-f31adf5ab280> in <module>
----> 1 data=pd.read_csv('Users\ergar\Desktop\ML-mini_bootcamp\W1')

~\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, 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, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
    674         )
    675 
--> 676         return _read(filepath_or_buffer, kwds)
    677 
    678     parser_f.__name__ = name

~\anaconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
    446 
    447     # Create the parser.
--> 448     parser = TextFileReader(fp_or_buf, **kwds)
    449 
    450     if chunksize or iterator:

~\anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, engine, **kwds)
    878             self.options["has_index_names"] = kwds["has_index_names"]
    879 
--> 880         self._make_engine(self.engine)
    881 
    882     def close(self):

~\anaconda3\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine)
   1112     def _make_engine(self, engine="c"):
   1113         if engine == "c":
-> 1114             self._engine = CParserWrapper(self.f, **self.options)
   1115         else:
   1116             if engine == "python":

~\anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, src, **kwds)
   1889         kwds["usecols"] = self.usecols
   1890 
-> 1891         self._reader = parsers.TextReader(src, **kwds)
   1892         self.unnamed_cols = self._reader.unnamed_cols
   1893 

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

FileNotFoundError: [Errno 2] File Users\ergar\Desktop\ML-mini_bootcamp\W1 does not exist: 'Users\\ergar\\Desktop\\ML-mini_bootcamp\\W1'

Why is this happening? How can I solve it? Thank you in advance for your time in clarifying this issue.

ergch24
  • 45
  • 4

3 Answers3

1

Try doing this, also assuming the name of your CSV file is W1:

data = pd.read_csv(r'C:\Users\ergar\Desktop\ML-mini_bootcamp\W1')
NYC Coder
  • 7,424
  • 2
  • 11
  • 24
  • Thank you. I'm coding in Windows/JupyterLab why now I have to write + 'r' at the start + The full path (from the actual folder)? I've been copying the path from JupyterLab and worked just fine, what should I do to avoid this and work directly on JupyterLab as I used to? – ergch24 Aug 25 '20 at 22:04
0

You must mention file extension, for example if yor file is .csv use:

data=pd.read_csv('Users\ergar\Desktop\ML-mini_bootcamp\W1.csv')
0

The issue is that you are giving a relative path to the Py interpreter. Relative paths are paths that do not start at the root of the filesystem - instead, as the name suggests, they are resolved relative to the location from which you call your program (in this case your notebook).

There are two ways you can solve this.

Option one is to use an absolute path - for Windows, start at C:\ or another appropriate drive letter - in your case, that would be

'C:\Users\ergar\Desktop\ML-mini_bootcamp\W1.csv'

Option two is to place your file in the same folder as your notebook and then use a relative path: pd.read_csv('W1.csv')

Note that you can create subfolders and organize your files that way - for instance you could create a datasets subfolder and then use pd.read_csv(r'datasets\W1.csv')

Also, remember to use r'path\to\file' (raw python strings) for Windows-style paths as '\' is a special character used for tings like new lines and tabs and could be misinterpreted when parsed as a non-raw string.

kiwibg
  • 334
  • 2
  • 9
  • Thank you, your answer was right also, but my question is why is this happening? since I always copied the path from JupyterLab and worked just fine, now y have to copy the path from the folder (making non sense to work on JupyterLab) and adding the r at the start, how can I assure to work as I used to? By the way, this is a file that I upload many times simply coding: `data=pd.read_csv('Users\ergar\Desktop\ML-mini_bootcamp\W1.csv')` – ergch24 Aug 25 '20 at 22:19
  • Well, the path like you wrote it is what is called a relative path (it doesn't start at the root of the filesystem). Meaning, if your notebook or lab server is running from, let's say C:\, it can find the path because there is a C:\Users\... - and if you are positioned somewhere else, it can't find the file. Imagine I tell you to go to Python street. If there is no Python street in your town you'll tell me there is no such street in your town. But if I tell you "Go to the city of Pandas, Python street", you'll know where to look for it. :) – kiwibg Aug 25 '20 at 22:25
  • Thanks agian, but to avoid that issue with relative path I have in the same folder the notebook and the dataset. Is that enough? Cause I've working that way and this is the first time I got that error. – ergch24 Aug 25 '20 at 22:28
  • I've updated the answer to fully cover what you were asking. I would appreciate it if you would mark it as an accepted answer. :) – kiwibg Aug 25 '20 at 22:55