简体   繁体   English

FileNotFoundError:[Errno 2] 没有这样的文件或目录:使用 csvreader

[英]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.我想使用 Google Colaboratory 上的 csvreader 读取一个 CSV 文件来模拟研究论文结果。 But I am getting the following error:但我收到以下错误:

FileNotFoundError: [Errno 2] No such file or directory: 'wind.csv' FileNotFoundError: [Errno 2] 没有这样的文件或目录:'wind.csv'

I have gone through a few articles suggesting how to import a CSV file in colaboratory.我浏览了几篇文章,建议如何在 colaboratory 中导入 CSV 文件。 This one sums it up pretty well Get Started: 3 Ways to Load CSV files into Colab这个总结得很好入门:将 CSV 文件加载到 Colab 的 3 种方法

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.我已经使用 Colab window 左侧的文件选项卡下的上传功能将我的文件放在 Colab 磁盘中。我对 pandas 了解不多,并且已经有了使用 csvreader 的预设代码。 So using pandas is not an option for me.所以使用 pandas 对我来说不是一个选择。

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. 上面的错误表明您没有将csv文件放置在放置代码文件的目录中。 Make sure that you have csv file in the same folder where you have the python code file. 确保csv文件位于python代码文件所在的文件夹中。

Two Reasons for this - 这有两个原因-

  1. You uploaded your 'wind.csv' in wrong directory (other than '/content'). 您将“ wind.csv”上传到了错误的目录(“ / content”除外)。
  2. Your current working directory is different than '/content'. 您当前的工作目录不同于'/ content'。 The default upload directory for Google Colab is '/content' unless you change it. 除非您进行更改,否则Google Colab的默认上传目录为“ / content”。

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 pwdpwd身份运行命令

it will show /content它会显示 /content

  1. Click on folder icon on left单击左侧的文件夹图标
  2. Then Right Click on Sample_data folder然后右键单击 Sample_data 文件夹
  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之后,您将在sample_data文件夹中看到该文件

Right-click on the file右键单击文件

click on copy path点击复制路径

put the full file in url command将完整文件放在 url 命令中

url = "paste path here"
  

example: url = "/content/sample_data/iris.csv"示例: 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反斜杠 (\\) -> 目标到 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.将该文件放在与 python 文件相同的级别或提供路径。

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.如果您打开脚本所在的终端/cmd,然后尝试运行它,否则您可以定义文件的完整路径,如果您从其他地方运行它,您将收到此错误,因此您可以执行其中任何一个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FileNotFoundError [Errno 2] 没有这样的文件或目录: - FileNotFoundError [Errno 2] No such file or directory: FileNotFoundError: [Errno 2] 没有这样的文件或目录? - FileNotFoundError: [Errno 2] No such file or directory? FileNotFoundError:[Errno 2]没有这样的文件或目录: - FileNotFoundError: [Errno 2] No such file or directory: FileNotFoundError: [Errno 2] 没有这样的文件或目录,但有一个文件 - FileNotFoundError: [Errno 2] No such file or directory but there is a file FileNotFoundError:[错误2]没有这样的文件或目录,但是文件在那里 - FileNotFoundError: [Errno 2] No such file or directory, But the file is there 获取“FileNotFoundError: [Errno 2] 没有这样的文件或目录” - Getting "FileNotFoundError: [Errno 2] No such file or directory" FileNotFoundError:[错误2]没有这样的文件或目录:'tesseract' - FileNotFoundError: [Errno 2] No such file or directory: 'tesseract' 错误:FileNotFoundError:[Errno 2]没有这样的文件或目录: - Error: FileNotFoundError: [Errno 2] No such file or directory: FileNotFoundError: [Errno 2] 没有这样的文件或目录:'hadoop' - FileNotFoundError: [Errno 2] No such file or directory: 'hadoop' 打开 FileNotFoundError: [Errno 2] 没有这样的文件或目录: - with open FileNotFoundError: [Errno 2] No such file or directory:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM