0

I am currently learning Pandas for data analysis and having some issues reading a csv file in Jupyter Notebook editor.

When I am running the following code:

import pandas as pd
df = pd.read_csv("C:\PythonHow\nycrime.csv")

I get an error message, which ends with

FileNotFoundError: File b'C:\\PythonHow\nycrime.csv' does not exist

and it's full lenght:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-36-75f2b616e06c> in <module>()
----> 1 lf2 = pd.read_csv("C:\PythonHow\nycrime.csv")

~\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

~\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:

~\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):

~\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':

~\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'C:\\PythonHow\nycrime.csv' does not exist

obs.: the file is in the directory with this exact name

0

3 Answers 3

1

As you can see here: FileNotFoundError: File b'C:\\PythonHow\nycrime.csv' does not exist the string \n is interpreted as a newline:

The workaround is:

df = pd.read_csv("C:\\PythonHow\\nycrime.csv")

Or:

df = pd.read_csv(r"C:\PythonHow\nycrime.csv")
Sign up to request clarification or add additional context in comments.

Comments

0

Please try using relative paths, it's always preferred :

  • more reliable / stable
  • easier to debug
  • easier to maintain
  • easier to distribute

Code :

import os
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'relative/path/to/file/you/want')

or

import os
path = os.getcwd() + '\relative\path\here.csv'

Also make sure whether your system uses /or \

That said, in your case, the error is probably due to the \ that are special characters in python. To use your string as a raw string and disable the processing of these special characters please use r"myPath" instead of "myPath"

1 Comment

Please up and mark as answer if this issue is closed
0

Sometimes, adding two backward slashes also solves the issue. Also adding 'r' before path will correct your path.

df = pd.read_csv(r"C:\\PythonHow\\nycrime.csv")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.