Unable to read a csv file

Hi,



I am trying to write a basic function from pandas o read a csv file stored on my local machine, however it gives me an error as below.

 

#import pandas library
import pandas as pd

# read the csv file
bhav20230707 = pd.read_csv("..\Downloads\bhav20230707.csv")
print(bhav20230707)

I also gave the full path (c:\users\Downloads\bhav20230707.csv) but the error persists. 



I also tried other methods and still I get the same error message. 



Error Message :

 

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In[4], line 5
      2 import pandas as pd
      4 # read the csv file
----> 5 bhav20230707 = pd.read_csv("..\Downloads\bhav20230707.csv")
      6 print(bhav20230707)

File ~\anaconda3\envs\quantra_py\lib\site-packages\pandas\io\parsers.py:610, in read_csv(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, storage_options)
    605 kwds_defaults = _refine_defaults_read(
    606     dialect, delimiter, delim_whitespace, engine, sep, defaults={"delimiter": ","}
    607 )
    608 kwds.update(kwds_defaults)
--> 610 return _read(filepath_or_buffer, kwds)

File ~\anaconda3\envs\quantra_py\lib\site-packages\pandas\io\parsers.py:462, in _read(filepath_or_buffer, kwds)
    459 _validate_names(kwds.get("names", None))
    461 # Create the parser.
--> 462 parser = TextFileReader(filepath_or_buffer, **kwds)
    464 if chunksize or iterator:
    465     return parser

File ~\anaconda3\envs\quantra_py\lib\site-packages\pandas\io\parsers.py:819, in TextFileReader.__init__(self, f, engine, **kwds)
    816 if "has_index_names" in kwds:
    817     self.options["has_index_names"] = kwds["has_index_names"]
--> 819 self._engine = self._make_engine(self.engine)

File ~\anaconda3\envs\quantra_py\lib\site-packages\pandas\io\parsers.py:1050, in TextFileReader._make_engine(self, engine)
   1046     raise ValueError(
   1047         f"Unknown engine: {engine} (valid options are {mapping.keys()})"
   1048     )
   1049 # error: Too many arguments for "ParserBase"
-> 1050 return mapping[engine](self.f, **self.options)

File ~\anaconda3\envs\quantra_py\lib\site-packages\pandas\io\parsers.py:1867, in CParserWrapper.__init__(self, src, **kwds)
   1864 kwds["usecols"] = self.usecols
   1866 # open handles
-> 1867 self._open_handles(src, kwds)
   1868 assert self.handles is not None
   1869 for key in ("storage_options", "encoding", "memory_map", "compression"):

File ~\anaconda3\envs\quantra_py\lib\site-packages\pandas\io\parsers.py:1362, in ParserBase._open_handles(self, src, kwds)
   1358 def _open_handles(self, src: FilePathOrBuffer, kwds: Dict[str, Any]) -> None:
   1359     """
   1360     Let the readers open IOHanldes after they are done with their potential raises.
   1361     """
-> 1362     self.handles = get_handle(
   1363         src,
   1364         "r",
   1365         encoding=kwds.get("encoding", None),
   1366         compression=kwds.get("compression", None),
   1367         memory_map=kwds.get("memory_map", False),
   1368         storage_options=kwds.get("storage_options", None),
   1369     )

File ~\anaconda3\envs\quantra_py\lib\site-packages\pandas\io\common.py:642, in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
    640         errors = "replace"
    641     # Encoding
--> 642     handle = open(
    643         handle,
    644         ioargs.mode,
    645         encoding=ioargs.encoding,
    646         errors=errors,
    647         newline="",
    648     )
    649 else:
    650     # Binary mode
    651     handle = open(handle, ioargs.mode)

OSError: [Errno 22] Invalid argument: '..\\Downloads\x08hav20230707.csv'

​
​

Can you please support me to get this resolved. I am desperately in need of help.



Thank you guru's!!



BR// Veera

Managed to find the solution.



Below code corrected the error

# import pandas library
import pandas as pd

# import csv file
bhav_today=pd.read_csv(r"C:\Users\veera\Downloads\fo07JUL2023bhav\fo07JUL2023bhav.csv")
bhav_today.head()

 

Hi,



It is great that you have resolved the error yourself. Do let us know if there is any other issue.

Until then Happy learning.

Thanks a lot for the helping hand. Defenitely I will.