Shuai Li
Shuai Li

Reputation: 2766

How to open local file on Jupyter?

In[1]:

path='/Users/apple/Downloads/train.csv'
open(path).readline()

Out[1]:

FileNotFoundError                         Traceback (most recent call 
last)
<ipython-input-7-7fad5faebc9b> in <module>()
----> 1 open(path).readline()

FileNotFoundError: [Errno 2] No such file or directory: 
'/Users/apple/Downloads/train.csv'

I'm confused a lot.I thought this code is exactly similar with many tutorials, and I'm sure I have this file in the right path, but why does it not works?

Upvotes: 33

Views: 342821

Answers (16)

y.kazmi
y.kazmi

Reputation: 1

I created a folder named Linear Regrassion as visible in the file browser image copy path. Then uploaded the .csv files alongwith the jupyter file (.ipynb) into that folder. Following this, this code allows the file to be read.

import os
import pandas as pd

train_file=os.path.abspath('Property_train (1).csv')
test_file=os.path.abspath('Property_test_share (1).csv')

ld_train=pd.read_csv(train_file)
ld_test=pd.read_csv(test_file)  

Then type in the file name in the abspath() as argument. Just the file name, not the folder name. or copy path but then delete the folder name copy path.

Works fine. Hope it helps.

Upvotes: 0

Victoralm
Victoralm

Reputation: 203

From within VSCode, using Jupyter extension. The "cereals.csv" was in the same folder as the notebook.

import os
import pandas as pd

cereals_csv = os.path.abspath("cereals.csv")

with open(cereals_csv) as file:
    myDf = pd.read_csv(file)
    display(myDf)

Upvotes: 0

Omar T.
Omar T.

Reputation: 21

The simplest way for avoiding confusion is to use a relative path.

You can do so by using ./ to have the current folder of your notebook as a root.

Please note that it will require you to have the .csv ( or other filetypes) located in the same folder or below the folder where the actual notebook is saved.

This is advised as good practice. If it's not possible for you to save your data close to the notebook then please DON'T follow the next steps.

Let's say that your Jupyter notebook is located in 'C:\Users\User123\python_programs\exampyle\exampyle_notebook.ipynb'

You can whether:

  1. directly save your my_data.csv in the same folder 'C:\Users\User123\python_programs\exampyle\my_data.csv'
path = './mydata.csv'
  1. be a little bit more tidy and put your data in a child folder 'C:\Users\User123\python_programs\exampyle\assets\my_data.csv'
path = './assests/mydata.csv'

Upvotes: 2

Dr. Alaa Temimy
Dr. Alaa Temimy

Reputation: 21

In windows , use windows explorer and go to the specified folder that contain your files. press shift + R.click > open windows power shell her > (run) Jupyter-lab (or) Jupyter notebook

in this case all the selected folder files will be in the Jupyter navigation pan. regards.

Upvotes: 0

Kidem
Kidem

Reputation: 11

On a windows system:

  1. Either open your new notebook in the same directory as the .csv file you downloaded
  2. Move your .csv file to the same directory
  3. Use open('C:\ \Users\ \stan\ \Desktop\ \ Examples_folder \ workingdata.csv') The double \ \ is important, didn't work for me without it. Pretty much use double \ everywhere else except right before your file name.

Note: No space between the double \

Upvotes: 1

VJ_99
VJ_99

Reputation: 1

On Windows you can use this code to read a local file stored in Parquet format: (Note that you might need to install 'pyarrow', if not already installed to be able to read Parquet files)

import pandas as pd
my_parquet = r'C:\Users\User123\Downloads\yellow.parquet'
df = pd.read_parquet(my_parquet)

Upvotes: -1

KASALA
KASALA

Reputation: 1

From my experience, In anaconda, create a environment and click on play symbol on that env and click open terminal. That's where you type cd path (if you store the file in C drive, copy the path of the drive and paste beside cd). After clicking enter write jupyter notebook. You'll see a chrome page opened.

Upvotes: -1

Kushal Atreya
Kushal Atreya

Reputation: 313

To start Jupyter Notebook in Windows:

  • open a Windows cmd (win + R and return cmd)
  • change directory to the desired file path (cd file-path)
  • give command jupyter notebook

You can further navigate from the UI of Jupyter notebook after you launch it (if you are not directly launching the right file.)
OR you can directly drag and drop the file to the cmd, to open the file.

C:\Users\kushalatreya>jupyter notebook "C:\Users\kushalatreya\Downloads\Material\PythonCourseFolder\PythonCourse-DataTypes.ipynb"

Upvotes: 3

ibodi
ibodi

Reputation: 1746

Install jupyter. Open terminal. Go to folder where you file is (in terminal ie.cd path/to/folder). Run jupyter notebook. And voila: you have something like this:

enter image description here

Notice that to open a notebook in the folder, you can either click on it in the browser or go to address:

http://localhost:8888/notebooks/name_of_your_file.ipynb

Upvotes: 9

Hao Xu
Hao Xu

Reputation: 627

simple way is to move your files to be read under the same folder of your python file, then you just need to use the name of the file, without calling another path.

Upvotes: 1

user4234032
user4234032

Reputation:

Here's a possibile solution (in Python):

Let's say you have a notebook with a file name, call it Notebook.ipynb. You are currently working in that notebook, and want to access other folders and files around it. Here's it's path:

import os
notebook_path = os.path.abspath("Notebook.ipynb")

In other words, just use the os module, and get the absolute path of your notebook (it's a file, too!). From there, use the os module and your path to navigate.

For example, if your train.csv is in a folder called 'Datasets', and the notebook is sitting right next to that folder, you could get the data like this:

train_csv = os.path.join(os.path.dirname(notebook_path), "Datasets/train.csv")
with open(train_csv) as file:
    #....etc

The takeaway is that the notebook has a file name, and as long as your language supports pathname manipulations (e.g. the os module in Python) you can likely use the notebook filename.

Lastly, the reason your code fails is probably because you're either trying to access local files (like your Mac's 'Downloads' folder) when you're working in an online Notebook (like Kaggle, which hosts your environment for you, online and away from your Mac), or you moved or deleted something in that path. This is what the os module in Python is meant to do; it will find the file's path whether it's on your Mac or in a Kaggle server.

Upvotes: 19

Reblochon Masque
Reblochon Masque

Reputation: 36662

On osX, Your path should be:

path = "/Users/name/Downloads/filename"

with name the current user logged in

Upvotes: 11

antadlp
antadlp

Reputation: 37

I do not know if it's what you were looking for, but it sounds to me something like this.

This is for linux (ubuntu) but maybe it also works on mac:

If the file is a pdf called 'book.pdf' and is located in your downloads, then

import subprocess

path='/home/user/Downloads/book.pdf'
subprocess.call(['evince', path])

where evince is the program that open pdfs in ubuntu

Upvotes: 0

Shuai Li
Shuai Li

Reputation: 2766

Many tutorials said that we should change Jupyter's workflow, but I didn't get it.

Finally, I find an easy way: Just drags file to this part.

enter image description here

Upvotes: 22

CY_
CY_

Reputation: 7618

I would suggest you to test it firstly: copy this train.csv to the same directory as this jupyter script in and then change the path to train.csv to test whether this can be loaded successfully.

If yes, that means the previous path input is a problem

If not, that means the file it self denied your access to it, or its real filename can be something else like: train.csv.<hidden extension>

Upvotes: 2

Vijith Vijayan
Vijith Vijayan

Reputation: 99

Are you running this on Windows or Linux? If you're on Windows,then you should be use a path like C:\\Users\\apple\\Downloads\train.csv . If you're on Linux, then you can follow the same path.

Upvotes: 9

Related Questions