ansh103
ansh103

Reputation: 11

FileNotFoundError: [Errno 2] No such file or directory: with csvreader

I want a CSV file to be read using csvreader on Google Colaboratory to emulate a research paper results. But I am getting the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'wind.csv'

I have gone through a few articles suggesting how to import a CSV file in colaboratory. This one sums it up pretty well Get Started: 3 Ways to Load CSV files into Colab

I have already placed my file in the Colab Disk using upload feature given under files tab on the small > present at the left side of Colab window. I don't know much about pandas and already have a preset code available using csvreader. So using pandas is not an option for me.

with open('wind.csv', 'r') as csvfile:
  reader = csv.reader(csvfile)
  rows = [row for row in reader]

I have placed file already in the drive and also copied the path by right-clicking(which was again the one I have given in the code above) I don't understand why the error is coming.

Upvotes: 1

Views: 70197

Answers (5)

Sharishth
Sharishth

Reputation: 1

If you open terminal/cmd where your script is located, and then try running it will work else you can define full path for your file, if you run it from somewhere else you will get this error so you can do either of these.

with open('C://..//your_path//wind.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    rows = [row for row in reader]

Upvotes: 0

Mano S
Mano S

Reputation: 831

df = pd.read_csv('C:/Users/WELCOME/Desktop/zomato.csv',encoding="ISO-8859-1")

Instead of

df = pd.read_csv('zomato.csv',encoding="ISO-8859-1")

Sometimes error occurs because of Back slash (\) that is default use must use back slash(/) for loading data.

back slash ( \ ) -> destination to root

front slash ( / ) -> root to destination

Upvotes: 1

Hashim Junaid
Hashim Junaid

Reputation: 1

Run the Command as pwd

it will show /content

  1. Click on folder icon on left
  2. Then Right Click on Sample_data folder
  3. Click upload file
  4. Search the file from your pc
  5. Click upload

After this, you will see the file in the sample_data folder

Right-click on the file

click on copy path

put the full file in url command

url = "paste path here"
  

example: url = "/content/sample_data/iris.csv"

Please note the uploaded file will be available till your session is active, once your session gets expired, the above steps have to be repeated again.....

I tried it and its works Good luck

Upvotes: 0

user11297110
user11297110

Reputation:

Two Reasons for this -

  1. You uploaded your 'wind.csv' in wrong directory (other than '/content').
  2. Your current working directory is different than '/content'. The default upload directory for Google Colab is '/content' unless you change it.

Use this -

import csv
%cd /content/

with open('wind.csv', 'r') as csvfile:
  reader = csv.reader(csvfile)
  rows = [row for row in reader]

print(rows)

See this - Screenshot

Upvotes: 0

Muhammad Adeel
Muhammad Adeel

Reputation: 79

The above error shows that you did not place the csv file in the same directory where the code file has been placed. Make sure that you have csv file in the same folder where you have the python code file.

Upvotes: 1

Related Questions