简体   繁体   中英

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.

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.

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

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

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

Your file isn't in the correct place which is why you are getting the error. Put the file on the same level as the python file or provide a path.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM