26

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?

2
  • Where is the server running?
    – Peter Wood
    Oct 27, 2017 at 9:56
  • It's a pity there is no possibility to right-click any folder in jupyter's "explorer view" and get a menu option to open the folder in the true os file explorer. This leads to convoluted tricks for files and folders operations. That said you finally found a way to drop the file in the header area. Good to know: On Windows you can also open the working directory using !explorer . command in a cell (explorer calls Windows explorer, and . sets the initial directory to the current one (else the documents folder is used). You can replace . by any valid path.
    – mins
    Apr 18, 2021 at 10:42

14 Answers 14

17

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.

16

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

1
  • So obvious that nobody could see! Ty :)
    – testing_22
    May 1, 2021 at 2:19
9

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.

1
  • 1
    In Windows 10, there was no need of using double backslashes i.e. I used C:\Users\apple\Downloads\train.csv and it worked fine.
    – MM Khan
    May 21, 2020 at 12:01
9

On osX, Your path should be:

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

with name the current user logged in

0
6

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
2

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>

2

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"
1

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.

1

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'
0

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

0

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)
0

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 \

2
  • 1
    Please edit your answer with proper formatting. The way it is right now it's not possible to see the double backslashes (`\`).
    – lepsch
    Jun 16, 2022 at 6:00
  • re: #3, aside from double backslashes you can also put "r" before the path, like r'C:\Users\stan' etc. It means to read it as a raw string. Things like \U have a special meaning in Python, that's why it didn't work.
    – Chuck
    Mar 9 at 14:44
0

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.

-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.

1
  • 2
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. Apr 21, 2022 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.