'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?
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[3]
On osX, Your path should be:
path = "/Users/name/Downloads/filename"
with name the current user logged in
Solution 4:[4]
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.
Solution 5:[5]
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:
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
Solution 6:[6]
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>
Solution 7:[7]
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.
Solution 8:[8]
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"
Solution 9:[9]
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
Solution 10:[10]
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)
Solution 11:[11]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | bytefish |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | Adil B |
| Solution 5 | ibodi |
| Solution 6 | CY_ |
| Solution 7 | Hao Xu |
| Solution 8 | Kushal Atreya |
| Solution 9 | antadlp |
| Solution 10 | |
| Solution 11 |


