0

I am trying to read a CSV file using pandas read_csv function but I keep getting an OSError. I have tried many different fixes but none of them seem to work. I have fixed a previous file not found error but unexpectedly got this error. Here is the code I used. Please note I am using Jupyter Notebook as part of Anaconda.

import pandas as pd
import os

curDir = os.getcwd()
#print(curDir)

melbourne_file_path = '..\Downloads\melb_data.csv'
melbourne_data = pd.read_csv(melbourne_file_path) 
print(melbourne_data.describe())

This is the output/error I get:

OSError                                   Traceback (most recent call last)
<ipython-input-2-12283d886215> in <module>()
      6 
      7 melbourne_file_path = '..\Downloads\melb_data.csv'
----> 8 melbourne_data = pd.read_csv(melbourne_file_path)
      9 print(melbourne_data.describe())

~\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)
    707                     skip_blank_lines=skip_blank_lines)
    708 
--> 709         return _read(filepath_or_buffer, kwds)
    710 
    711     parser_f.__name__ = name

~\Anaconda3\lib\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\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\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\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
OSError: Initializing from file failed
H D
  • 81
  • 1
  • 18
  • Your file either missing or inaccessible. Try `with open(melbourne_file_path): pass` before call to read_csv – Muposat Mar 26 '18 at 19:57
  • I get a permission error now. `PermissionError: [Errno 13] Permission denied: '..\\Downloads\\melb_data.csv'` – H D Mar 27 '18 at 19:58
  • That's the real error. Find the file in Explorer and see if you can fix permissions. Maybe you run the program as a different user, and that user does not have access to the file. – Muposat Mar 27 '18 at 21:05
  • I am able to open the file with no restriction of access from File Explorer. There are also no specific permissions and my PC has only one account that is the admin therefore I should be able to get in no matter what. – H D Mar 28 '18 at 18:53
  • Maybe you don't have permission for `..` or for `Downloads`? Also you did not escape backslash, so that could be an issue. Experiment with moving file to local dir, copying it under new name, etc. Should be super-trivial to troubleshoot. – Muposat Mar 28 '18 at 19:46
  • I have moved the file within the same folder but still get the same error. What do you mean I did not escape backslash? – H D Mar 28 '18 at 20:08
  • https://stackoverflow.com/questions/19095796/how-to-print-backslash-with-python – Muposat Mar 28 '18 at 20:34

0 Answers0