Python Forum

Full Version: Read csv file through PyCharm
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear Madam/Sir,

I am beginner to learn python programming through Pycharm. I tried to import csv file thorough Pycharm and current working directory is ‘C:\Users\hakjo\PycharmProjects\jm1’ and python file name is ‘main.py’. When I run the following program, I repeatedly have error messages.
I read many postings related to relative vs. absolute path and I don’t see any problem to set the absolute path where ‘sentiment-test.crv’ file is stored. It will be highly appreciated if you can help me to fix this error.

import pandas as pd
df=pd.read_csv(r’C:\Users\hakjo\PycharmProjects\jm1\sentiment-test.crv’)

C:\Users\hakjo\PycharmProjects\jm1\venv\Scripts\python.exe C:/Users/hakjo/PycharmProjects/jm1/main.py
Traceback (most recent call last):
** File “C:\Users\hakjo\PycharmProjects\jm1\main.py”, line 6, in **
** df=pd.read_csv(r’C:\Users\hakjo\PycharmProjects\jm1\sentiment-test.crv’)**
** File “C:\Users\hakjo\PycharmProjects\jm1\venv\lib\site-packages\pandas\util_decorators.py”, line 311, in wrapper**
** return func(args, kwargs)
** File “C:\Users\hakjo\PycharmProjects\jm1\venv\lib\site-packages\pandas\io\parsers\readers.py”, line 586, in read_csv*
** return _read(filepath_or_buffer, kwds)**
** File “C:\Users\hakjo\PycharmProjects\jm1\venv\lib\site-packages\pandas\io\parsers\readers.py”, line 482, in _read**
** parser = TextFileReader(filepath_or_buffer, kwds)
** File “C:\Users\hakjo\PycharmProjects\jm1\venv\lib\site-packages\pandas\io\parsers\readers.py”, line 811, in init**
** self._engine = self._make_engine(self.engine)**
** File “C:\Users\hakjo\PycharmProjects\jm1\venv\lib\site-packages\pandas\io\parsers\readers.py”, line 1040, in _make_engine**
** return mapping[engine](self.f, self.options) # type: ignore[call-arg]
** File “C:\Users\hakjo\PycharmProjects\jm1\venv\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py”, line 51, in init**
** self._open_handles(src, kwds)**
** File “C:\Users\hakjo\PycharmProjects\jm1\venv\lib\site-packages\pandas\io\parsers\base_parser.py”, line 222, in _open_handles**
** self.handles = get_handle(**
** File “C:\Users\hakjo\PycharmProjects\jm1\venv\lib\site-packages\pandas\io\common.py”, line 701, in get_handle**
** handle = open(**
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\hakjo\PycharmProjects\jm1\sentiment-test.crv’

Process finished with exit code 1

Thank you so much
Joon
Hi Joon,
Please read BBCode to learn how to post code and output.
Error:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\hakjo\PycharmProjects\jm1\sentiment-test.crv’
The message seems quite clear. Does the file "C:\Users\hakjo\PycharmProjects\jm1\sentiment-test.crv" exist and is it readable? Can you open it with notepad.exe? Should the name not be "C:\Users\hakjo\PycharmProjects\jm1\sentiment-test.csv"?
You can test the file exists,

import os
import pandas as pd
myfile = r"C:\Users\hakjo\PycharmProjects\jm1\sentiment-test.crv"
if os.path.isfile(myfile):
    df=pd.read_csv(myfile)
    print(df)
else:
    print(f"file {myfile} does not exist")
maybe you can also use

myfile = "C:/Users/hakjo/PycharmProjects/m1/sentiment-test.crv"