Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File b'train.csv' does not exist even though file exist

Code:

import pandas as pd
train_df = pd.read_csv("train.csv")

Error:

FileNotFoundError                   Traceback (most recent call last)
    <ipython-input-17-05c7c432b69f> in <module>()
      1 import pandas as pd
      2 
    ----> 3 train_df = pd.read_csv("../input/train.csv")

    /anaconda3/lib/python3.6/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)
    707                     skip_blank_lines=skip_blank_lines)
    708 
    --> 709         return _read(filepath_or_buffer, kwds)
    710 
    711     parser_f.__name__ = name

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

    /anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
    816             self.options['has_index_names'] = kwds['has_index_names']
    817 
    --> 818         self._make_engine(self.engine)
    819 
    820     def close(self):

    /anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py in _make_engine(self, engine)
    1047     def _make_engine(self, engine='c'):
    1048         if engine == 'c':
    -> 1049             self._engine = CParserWrapper(self.f, **self.options)
    1050         else:
    1051             if engine == 'python':

    /anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
    1693         kwds['allow_leading_cols'] = self.index_col is not False
    1694 
    -> 1695         self._reader = parsers.TextReader(src, **kwds)
    1696 
    1697         # XXX

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

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

FileNotFoundError: File b'../input/train.csv' does not exist

Please help! I tried using pd.read_csv("../input/train.csv") but there is still an error. I am a Mac user using Jupyter notebook.

like image 563
Yun Jie Lim Avatar asked Jun 06 '18 13:06

Yun Jie Lim


4 Answers

  1. Are you sure you have the correct path?

train_df = pd.read_csv("./input/train.csv") (if the csv file is in the input folder which is in the same folder as your jupyter notebook)

Easiest would be you have a folder which contains the juptyer notebook and the csv file. Then you would just need to do:

train_df = pd.read_csv("./train.csv") or train_df = pd.read_csv("train.csv")

  1. Try using train_df = pd.read_csv("train.csv",encoding='utf-8' )

to get rid of the 'b in front of b'../input/train.csv'

like image 102
Julia Avatar answered Sep 29 '22 13:09

Julia


Try using an absolute path like this. The r at the beginning of the line helps to read the whole string as a raw string as it is, so when r is used, you don't have to worry about escaping slashes

import pandas

myFile = pandas.read_csv(r"C:\Users\samarnat\Documents\Personal Docs\Projects\train.csv",encoding='utf-8')
like image 36
Sandeep Amarnath Avatar answered Sep 29 '22 13:09

Sandeep Amarnath


I faced exact problem yesterday, following points helped me to resolve the problem

  1. No combination filePath was working out for me.
  2. I manually added full path to file and it worked for me.

So now I have added single line of code to my code and it works all the time regardless of folder structure. Providing absolute path of file is the key.

csv_file_abs = os.path.abspath(csv_file)
print(f'ABS PATH: {csv_file_abs}')
df = pd.read_csv(csv_file_abs)

I hope this will help, please share your thoughts if this problem can be solved in other ways, this was quick-fix.

like image 21
Sourabh Desai Avatar answered Sep 29 '22 12:09

Sourabh Desai


Possible Reasons:

  1. Path entered is incorrect or multiple folders of same name.
  2. The name of the file is incorrect.
  3. The file extension is not csv, it maybe xlsx (Rare chance)

Hope this helps.

like image 42
Nisarg Bhatt Avatar answered Sep 29 '22 11:09

Nisarg Bhatt