close

How to open local file on Jupyter?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to open local file on Jupyter in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to open local file on Jupyter?

  1. How to open local file on Jupyter?

    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.

  2. open local file on Jupyter

    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.

Method 1

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.

Method 2

On osX, Your path should be:

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

with name the current user logged in

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read