简体   繁体   中英

File not found in jupyter notebook for python?

I'm trying to pull datasets into jupyter notebook from a CSV file, but the file is not found. I'm not sure exactly how to make the file available to be found.

I have tried naming all of the directories, but get confused.

Heres my code:

import pandas as pd

df = pd.read_csv("TrainLabel1.csv")

df.head()

I need the data to be pulled to show up accurately, please help me on how to do it.

try using the os module to join the current working directory to the filename if that doesn't work then check to make sure you have the file in the same directory as your python file

import os
import pandas as pd

filename = os.path.join(os.getcwd(), 'TrainLabel1.csv')

df = pd.read_csv(filename)
df.head()

By default, the read_csv will read the file in the directory where the python/jupyter was launched. So make sure the file was in the same location else just provide the whole path of the file location and also make sure to keep the file in place if you are using a remote notebook.

When the file was in the same directory where python shell was lanced

>>> import pandas as pd
>>> pd.read_csv("xyz.csv")
          id           expired_by
0       18001604904  2018-11-05 20:37:31
1       21001745601  2018-11-05 20:37:38
2  3006807007209405  2018-11-05 20:37:48
3  3010363003951901  2018-11-05 20:37:58
4  3010363003951701  2018-11-05 20:48:10
5  3010363003951702  2018-11-05 20:48:14
6  3010363003951703  2018-11-05 20:48:17

if the file is in a different path

>>> import pandas as pd
>>> df = pd.read_csv("/Users/x/y/z.csv")
>>> df.head()
          id           expired_by
0       18001604904  2018-11-05 20:37:31
1       21001745601  2018-11-05 20:37:38
2  3006807007209405  2018-11-05 20:37:48
3  3010363003951901  2018-11-05 20:37:58
4  3010363003951701  2018-11-05 20:48:10
5  3010363003951702  2018-11-05 20:48:14
6  3010363003951703  2018-11-05 20:48:17

This is has nothing to do with the jupyter notebook unless the jupyter is running on a remote machine then you make sure you sftp the file to that server

您可以像这样使用原始文件路径

df = pd.read_csv(r'C:\\Users\\me\\Anaconda3\\fr_FR.csv')

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