3

I'll try to be as simple as I can be. I'm not great at these things.

On my computer, at the location "C:\Users\Oria" there's a folder called Project. That folder contains code.ipynb, and a folder called data. Inside the folder data, there's just one file called iris_features.csv

I uploaded code.ipynb to jupyter notebook, there's a line there (which is locked to changes, can't change it) which reads

irisCsvFileName = 'data' + os.sep + 'iris_fearures.csv'
df_iris_features = pd.read_csv(irisCsvFileName)

So from what I understand, it should understand that the working directory is "C:\Users\Oria\Project" and all paths will be relative to it.

However, it doesn't work. It gives the error

FileNotFoundError: [Errno 2] File data\iris_fearures.csv does not exist: 'data\\iris_fearures.csv'

When I give the full path of the iris_features.csv, it works fine. However, as I said, I can't change the given code.

What am I doing wrong? Should I upload more than just the ipynb file to jupyter notebook?

Oria Gruber
  • 1,513
  • 2
  • 22
  • 44

2 Answers2

3

There's a typo in the code you've provided in your question:

irisCsvFileName = 'data' + os.sep + 'iris_fearures.csv'
df_iris_features = pd.read_csv(irisCsvFileName)

You've written iris_fearures.csv but later have said that the file is called iris_features. You can check your current working directory is what you expect using:

import os
cwd = os.getcwd()

And you can find more information on using file paths etc in this SO answer

Jason
  • 4,346
  • 10
  • 49
  • 75
  • it's a typo in the question, the names are actually fine. – Oria Gruber Mar 21 '20 at 16:49
  • @OriaGruber Are you sure, as it's also in the error message your provided – Jason Mar 21 '20 at 16:51
  • 1
    It seems the writer of the assignment was sloppy but consistent, it's all fearures and not features. regardless, i got it to work using the os cwd trick. Thanks! – Oria Gruber Mar 21 '20 at 16:55
  • @OriaGruber Glad you solved your problem. `import glob, os; cwd = os.getcwd(); print(glob.glob(cwd+os.sep+'*'))` Can also be a handy check to print all files in your current working directory, especially if you need to do something all `.csv`s etc, then you can save a list and iterate through that list without having to worry about spelling/typos. – Jason Mar 21 '20 at 17:00
2

you have to give the full path if you didn't open the jupyter-notebook from the folder C:\Users\Oria\Project, if you just open a .ipynb from same folder Project the paths will not be relative to that .ipynb but with the folder from where you start the jupyter

you can check the current working directory (to whom all the other paths are relatives if they are not full paths):

import os

os.getcwd()
kederrac
  • 16,819
  • 6
  • 32
  • 55