1

I have to open an excel file, and I do in this way:

 xl_file = pd.ExcelFile('D:\mypath\myFile.xls')

On PyCharm(Python 2.7.8) it works perfectly, but on Jupyter(Python 3), I've always this error:

FileNotFoundError: [Errno 2] No such file or directory

What could be the reason?

Joe
  • 9,817
  • 4
  • 32
  • 43

2 Answers2

1

This might happen if you call jupyter notebook at a place other than your root directory. In this case, jupyter might not have access to the file.

Try going to D: and calling jupyter notebook and then retrying this. Another option is to get the path of your notebook using:

os.path.abspath("__file__")

and then setting a relative path to the dataset.

Edit:

Let's say you want to set a path one level above the directory that contains the notebook. Then you would do:

foo = os.path.dirname(os.path.abspath("__file__"))
relative_path = os.path.join(foo, '..')
amanbirs
  • 978
  • 5
  • 11
  • I call Jupyter from Coursera website, so I dont think there is much I can do about the first option.How do I set a relative path? – Joe Nov 11 '17 at 13:37
  • Ahhh. So is jupyter running on the coursera servers? That might be the root of your problem. – amanbirs Nov 11 '17 at 13:44
  • If the jupyter notebook is running on coursera servers, then this is equivalent of it running on another computer. You can use the notebook through your browser, but cannot access files on your own machine. If this dataset is part of an assignment then I'm guessing that this should be accessible on their server in some way. I'd suggest checking the assignment instructions. If you still cannot figure it out, post a link to the instructions here. – amanbirs Nov 11 '17 at 14:17
  • Ive followed these instructions: https://stackoverflow.com/questions/35254852/how-to-change-jupyter-start-folder but still it does not work – Joe Nov 11 '17 at 15:34
  • 1
    There seems to be a fundamental misunderstanding here. The problem is not that you cannot change the folder. The problem is that your dataset and jupyter notebook are running on two different computers. If you open the terminal or command prompt on your local machine and type jupyter notebook - then you should be able to follow the instructions you linked to. – amanbirs Nov 11 '17 at 20:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158781/discussion-between-amanbirs-and-giuseppe). – amanbirs Nov 11 '17 at 20:27
0

After having changed the Jupyter start folder as suggested in this post how to change jupyter start folder? , if the files are in this folder, to load them it isnt necessary to write the path. This is enough:

xl_file = pd.ExcelFile('myFile.xls')
Joe
  • 9,817
  • 4
  • 32
  • 43