blue
blue

Reputation: 11

Python shell csv problem FileNotFoundError: [Errno 2] No such file or directory: 'iris.csv'

iris_df = pd.read_csv("iris.csv")

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    iris_df = pd.read_csv("iris.csv")
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\parsers.py", line 610, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\parsers.py", line 462, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\parsers.py", line 819, in __init__
    self._engine = self._make_engine(self.engine)
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\parsers.py", line 1050, in _make_engine
    return mapping[engine](self.f, **self.options)  # type: ignore[call-arg]
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\parsers.py", line 1867, in __init__
    self._open_handles(src, kwds)
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\parsers.py", line 1362, in _open_handles
    self.handles = get_handle(
  File "C:\Python 3.8.1\lib\site-packages\pandas\io\common.py", line 642, in get_handle
    handle = open(
FileNotFoundError: [Errno 2] No such file or directory: 'iris.csv'

What should i do? I imported pandas as pd but it gives me error.Please help

Upvotes: 1

Views: 18075

Answers (2)

megurobi
megurobi

Reputation: 11

You can also write these two lines of code at the top of your file:

import os
os.chdir(os.path.dirname(__file__))

This will change your working directory to the correct path, whatever it was initially.

Upvotes: 0

Azhan Mohammed
Azhan Mohammed

Reputation: 424

That's probably because you don't have the file in the exact folder as your code file. Try keeping them both in the same folder and try this:

import pandas as pd
iris_df = pd.read_csv('./iris.csv')

Or you can also try copying the exact path to your iris.csv file and then load it using:

iris_df = pd.read_csv(' #path to your iris.csv  file')

For relative path the ./iris.csv tells the function to look in the same folder as that of the code file for the iris.csv file. This however won't work if your iris.csv file is in another folder. In that case: If you are on a Linux based system your exact path would be something like:

home/usr/Desktop/FolderName/iris.csv

For a Windows system the absolute path would be something like:

C:/Desktop/FolderName/iris.csv

Upvotes: 1

Related Questions