File Directory in Jupyter Notebook

Hello,

I am trying to open a file in jupyter notebook however i am getting the below error when i try to read as a list of lists.

I need to know what has gone wrong?
How can i know the directory of the notebook so i can paste the file to notebook directly or start a new notebook in the directory of the file i want to open?

Thanks,
Adel.

What I do is keep my data on my desktop along with where I’m going to keep the script and it work fine for me just by putting “name.csv”

The key is just to make sure the source file and the python jupyter notebook file are in the same folder. As long as they are together, we can read with ‘name.csv’

Based on your file path it looks like you’re using Windows? In that case I would include a r in front of the file path so that Python interprets it as a raw string.

So it would look like:
open_file = open(r’D:\DataQuest\Python Data Analyst\Project_1\AppleStore.csv’)

The backslash ( ‘\’ ) is an escape character for strings so you either have to escape it with another backslash or convert to a raw string so that the file path is read properly.

Hope this helps!

1 Like

I create a set folder for Jupyter notebooks on the machine I’m working, uncreatively I call that folder “Notebooks”.
I then generate a jupyter config file by running this command in cmd/terminal

jupyter notebook --generate-config

In windows this places the config file ‘jupyter_notebook_config.py’ in your c:\users[username].jupyter folder
On a mac(and I suspect linux) this goes in your /users/[username]/.jupyter directory.

I then edit this config to set the default path that jupyter uses to find notebooks. You do this by editing the setting labelled as follows:

c.NotebookApp.notebook_dir

ensure you use forward slashes in the directory path, not backwards.

Now when you load jupyter this will “land” in that folder you setup and you can be super organised.

If you want to take it a step further, I would also recommend using CookieCutter to build your new project folder structure each time. Instructions can be found here: https://github.com/drivendata/cookiecutter-data-science

A good template/structure to used can be called like so on your command line (Guide here)

cookiecutter https://drivendata.github.io/cookiecutter-data-science/

The benefit of cookiecutter is that you’ll use a standard directory structure which is well organised. You’ll have place to store your initial raw data, your worked on data, and your final output for example. All very important so you can always recreate your experiment.

1 Like

I don’t see how this is useful.

@alvinctk

I don’t see how this is useful.

Assuming you are replying to me, my answer is providing a solution to the users root cause of the problem. Simply telling them the bit of code to finding their file for that notebook isn’t solving the underlying problem that @adel.abdallah2790 has. The OP doesn’t even know where they jupyter notebook is stored on their own system even though they are working on it.

This is a fundamental bit of learning. The user should know exactly where their work files are stored otherwise their development and tests cannot be shared and reproduced. You should also learn how to control your jupyter environment to work for you. Any data analyst and I’m sure data scientist will agree that organised and consistent approach to how you present your work is very important.

My post is helping them to setup their jupyter notebooks in their own desired location, and cookiecutter assists them to be organised with their jupyter notebook development into a layout that is well organised and well used by other people.

If I wanted to provide an exact answer to only this posted problem then I’d point out that the problem is with the encoding of the file, not the location of the file.

Hello Adel, try to use this code below

from csv import reader
opened_file = open(‘file name.csv’)
read_file = reader(opened_file)
app_data = list(read_file)

You do not need to know where your laptop projects are located in order to transfer the file to the project folder. You can use Drag-on-Drop and simply drag and drop the file into the browser’s open window in jupyter

It should look like this

But your problem is not with the file path, but with the encoding of the data in the file.
Try explicitly specifying the required encoding when opening the file

open(file, encoding = 'UTF-8')
1 Like