在 Jupyter 笔记本中使用 Pandas 导入 csv 文件时出现 FileNotFoundError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46643768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:36:22  来源:igfitidea点击:

FileNotFoundError while importing a csv file using pandas in Jupyter notebook

pythonpandascsvimport-csv

提问by Josep M Domingo

Screenshot of the described error.

所描述错误的屏幕截图。

import pandas as pd
df = pd.read_csv('/home/josepm/Documents/test_ver2.csv')

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-3-5cd7fd573fb7> in <module>()
      1 import pandas as pd
----> 2 df = pd.read_csv('/home/josepm/Documents/test_ver2.csv')

I try to import a CSV file using pandas and every time it says that it doesn't find the file. It's like Jupyter doesn't see it. I tried to do this:

我尝试使用 Pandas 导入一个 CSV 文件,但每次它都说找不到该文件时。就像 Jupyter 没有看到它。我试图这样做:

import os
os.path.isfile('/home/josepm/Documents/test_ver2.csv')

and it doesn't see the file either.

它也没有看到该文件。

采纳答案by Chankey Pathak

Change

改变

pd.read_csv('\Users\user\Desktop\Workbook1.csv')

to

pd.read_csv(r'C:\Users\user\Desktop\Workbook1.csv')

回答by S. HU

Please try the following code:

请尝试以下代码:

import os  
path = os.path.abspath(r'file path')
f = open(path)
print(f)

回答by S. HU

The working directory is the point from where all the files are accessed in Jupyter Notebook.

Find the current working directory

import os

os.getcwd()

Example o/p : 'C:\Users\xyz'

Now place your CSV files in this path

List the contents of your directory to check if the CSV file is present

os.listdir('C:\Users\xyz')

Now try reading the CSV file

工作目录是 Jupyter Notebook 中所有文件的访问点。

查找当前工作目录

导入操作系统

os.getcwd()

示例 o/p : 'C:\Users\xyz'

现在将您的 CSV 文件放在此路径中

列出目录的内容以检查 CSV 文件是否存在

os.listdir('C:\Users\xyz')

现在尝试读取 CSV 文件