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?
This question is related to
python
jupyter-notebook
jupyter
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.
On osX
, Your path should be:
path = "/Users/name/Downloads/filename"
with name
the current user logged in
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:
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
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>
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.
To start Jupyter Notebook in Windows:
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"
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
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.
Source: Stackoverflow.com