1

So I am trying to import a csv file with python through pandas. The code is

import pandas as pd

df = pd.read_csv(r"C:\Users\B1880\Downloads\avocado-prices.zip\avocado.csv")

though I keep getting the error:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-106-03cc5a9f89de> in <module>
      1 import pandas as pd
      2 
----> 3 df = pd.read_csv(r"C:\Users\B1880\Downloads\avocado-prices.zip\avocado.csv")
      4 d

~\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 C:\Users\B1880\Downloads\avocado-prices.zip\avocado.csv does not exist: 'C:\\Users\\B1880\\Downloads\\avocado-prices.zip\\avocado.csv'

I dont understand how it does not find the file when I know that I have the correct entire file path. I went to the search bar in the bottom right of windows, then I looked up the file and copied the full path. I have tried with the r infront of the file and without it. I tried double backslash and single. How can it not find the file if that is its path?

Bentio cano
  • 53
  • 1
  • 6
  • looks like you are trying to access a file within a `*.zip` file. You might want to try an unzip the file first. Or, just try reading the zip file directly. – monkut May 28 '20 at 00:01
  • I believe pandas should be able to read a zipped `.csv` file if the filenames are the same. For example `avocado.csv` -> `avocado.zip` – monkut May 28 '20 at 00:02
  • @monkit thank you for responding. How would I do either of those things? – Bentio cano May 28 '20 at 00:03
  • 1
    THANK YOU SOOOO MUCH!!!!! you were right all I had to do was unzip the file! THANK OU THANK YOU. (sorry I have been at this for hours :) ) – Bentio cano May 28 '20 at 00:07

1 Answers1

1

try using the zipfile module mentioned here -> Reading csv zipped files in python

Something like this should work:

import pandas as pd
import zipfile

zf = zipfile.ZipFile('C:\Users\B1880\Downloads\avocado-prices.zip') 
df = pd.read_csv(zf.open('avocado.csv'))
NYC Coder
  • 7,424
  • 2
  • 11
  • 24