user3120266
user3120266

Reputation: 425

Trying to load file into Python but it states that it does not exist even though it does

I am trying to load a csv file into Pandas. I am getting a weird error that I have never encountered before that the file does not exist even though it does. The error also calls the file a different name in the message "ree.csv"

import pandas as pd
tree = pd.read_csv('C:\Users\Desktop\tree.csv')

IOError                                   
Traceback (most recent call last)
<ipython-input-63-aed3b96442f2> in <module>()
----> 1 tree = pd.read_csv('C:\Users\Desktop\tree.csv')

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in parser_f(filepath_or_buffer, sep, dialect, compression, doublequote, escapechar, quotechar, quoting, skipinitialspace, lineterminator, header, index_col, names, prefix, skiprows, skipfooter, skip_footer, na_values, na_fvalues, true_values, false_values, delimiter, converters, dtype, usecols, engine, delim_whitespace, as_recarray, na_filter, compact_ints, use_unsigned, low_memory, buffer_lines, warn_bad_lines, error_bad_lines, keep_default_na, thousands, comment, decimal, parse_dates, keep_date_col, dayfirst, date_parser, memory_map, nrows, iterator, chunksize, verbose, encoding, squeeze, mangle_dupe_cols, tupleize_cols)
    398             )
    399 
--> 400         return _read(filepath_or_buffer, kwds)
    401 
    402     parser_f.__name__ = name

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in _read(filepath_or_buffer, kwds)
    196 
    197     # Create the parser.
--> 198     parser = TextFileReader(filepath_or_buffer, **kwds)
    199 
    200     if nrows is not None:

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in __init__(self, f, engine, **kwds)
    477             self.options['has_index_names'] = kwds['has_index_names']
    478 
--> 479         self._make_engine(self.engine)
    480 
    481     def _get_options_with_defaults(self, engine):

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in _make_engine(self, engine)
    584     def _make_engine(self, engine='c'):
    585         if engine == 'c':
--> 586             self._engine = CParserWrapper(self.f, **self.options)
    587         else:
    588             if engine == 'python':

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\io\parsers.pyc in __init__(self, src, **kwds)
    955         kwds['allow_leading_cols'] = self.index_col is not False
    956 
--> 957         self._reader = _parser.TextReader(src, **kwds)
    958 
    959         # XXX

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\parser.pyd in       pandas.parser.TextReader.__cinit__ (pandas\parser.c:2987)()

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\parser.pyd in pandas.parser.TextReader._setup_parser_source (pandas\parser.c:5345)()

IOError: File C:\Users\Desktop  ree.csv does not exist

Upvotes: 1

Views: 8833

Answers (3)

Andy
Andy

Reputation: 50600

The problem is how your path is read.

tree = pd.read_csv('C:\Users\Desktop\tree.csv')

From this, the \t treated as a tab character.

You have 3 solutions available though:

Option 1 - Utilize raw strings:

tree = pd.read_csv(r'C:\Users\Desktop\tree.csv')

When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string.

Option 2 - Utilize double quotes. This escapes the \ so that you pass a proper path.

tree = pd.read_csv('C:\\Users\\Desktop\\tree.csv')

Option 3 - Change the \ to /:

tree = pd.read_csv('C:/Users/Desktop/tree.csv')

All three of these will provide a correct path that can be utilized.

Upvotes: 1

Dan Lenski
Dan Lenski

Reputation: 79782

You are not giving Python the filename that you think you are, because backslash characters in strings are interpreted as special escape characters.

Use raw strings which do not treat backslashes as escape characters:

r'C:\Users\Desktop\tree.csv'

Upvotes: 0

Peter Pei Guo
Peter Pei Guo

Reputation: 7870

Try this:

'C:/Users/Desktop/tree.csv'

Or

'C:\\Users\\Desktop\\tree.csv'

Upvotes: 5

Related Questions