user8270077
user8270077

Reputation: 5071

Reading csv files in Pandas

I must read a list of files that happen to have in their names a fullpoint:

['0001.HK.csv',
 '0002.HK.csv',
 '0003.HK.csv',
 '0004.HK.csv',
 '0005.HK.csv',
 '0006.HK.csv',...]

When I try to read the file with pandas I get an error message:

df = pd.read_csv('001.HK.csv', index_col = 0)


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-232-a710be5c9b60> in <module>
----> 1 df = pd.read_csv('001.HK.csv', index_col = 0)

~\Anaconda3\envs\tf2\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\envs\tf2\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\envs\tf2\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\envs\tf2\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\envs\tf2\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 001.HK.csv does not exist: '001.HK.csv'

What would be the most efficient way to read these files in pandas?

Upvotes: 0

Views: 99

Answers (1)

Edgar Domingues
Edgar Domingues

Reputation: 990

In the list the files have 4 digits number but you are trying to open a file with only 3 digits number. Check if the name of the files have 3 or 4 digits and open the correct one.

From the error it looks like the file 001.HK.csv doesn't exist in the current working directory. Check the directory where you are running the script and if that file exists there.

Upvotes: 1

Related Questions