Python 如何在 Jupyter 上打开本地文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46972225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open local file on Jupyter?
提问by bitfishxyz
In[1]:
在[1]:
path='/Users/apple/Downloads/train.csv'
open(path).readline()
Out[1]:
出[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?
我很困惑。我认为此代码与许多教程完全相似,并且我确定我在正确的路径中放置了此文件,但是为什么它不起作用?
回答by bitfishxyz
回答by Reblochon Masque
On osX
, Your path should be:
在osX
,你的路径应该是:
path = "/Users/name/Downloads/filename"
with name
the current user logged in
使用name
当前用户登录
回答by Vijith Vijayan
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.
你是在 Windows 还是 Linux 上运行这个?如果您使用的是 Windows,那么您应该使用类似C:\\Users\\apple\\Downloads\train.csv
. 如果您使用的是 Linux,那么您可以遵循相同的路径。
回答by Vijith Vijayan
Here's a possibile solution (in Python):
这是一个可能的解决方案(在 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:
假设您有一个带有文件名的笔记本,将其命名为 Notebook.ipynb。您当前正在该笔记本中工作,并且想要访问它周围的其他文件夹和文件。这是它的路径:
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.
换句话说,只需使用 os 模块,并获取笔记本的绝对路径(它也是一个文件!)。从那里,使用 os 模块和您的路径进行导航。
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 位于名为“Datasets”的文件夹中,并且笔记本就位于该文件夹旁边,您可以获得如下数据:
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.
要点是笔记本有一个文件名,只要您的语言支持路径名操作(例如 Python 中的 os 模块),您就可以使用笔记本文件名。
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.
最后,您的代码失败的原因可能是因为您在使用在线笔记本(例如 Kaggle,它为您托管您的环境,在线和离开您的 Mac),或者您移动或删除了该路径中的某些内容。这就是 Python 中的 os 模块的目的;它会找到文件的路径,无论它是在你的 Mac 上还是在 Kaggle 服务器中。
回答by antadlp
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:
这是针对 linux (ubuntu) 但也许它也适用于 mac:
If the file is a pdf called 'book.pdf' and is located in your downloads, then
如果该文件是名为“book.pdf”的 pdf 并且位于您的下载中,则
import subprocess
path='/home/user/Downloads/book.pdf'
subprocess.call(['evince', path])
where evince is the program that open pdfs in ubuntu
其中 evince 是在 ubuntu 中打开 pdf 的程序
回答by Kelvin
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.
我建议你先测试一下:把这个复制train.csv
到这个jupyter脚本所在的目录下,然后把路径改成train.csv
测试是否可以成功加载。
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>
如果不是,这意味着它自己拒绝您访问它的文件,或者它的真实文件名可以是其他类似的东西: train.csv.<hidden extension>
回答by ibodi
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:
安装 jupyter。打开终端。转到文件所在的文件夹(在终端中,即。cd path/to/folder
)。运行jupyter notebook
。瞧:你有这样的事情:
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
回答by Hao Xu
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.
简单的方法是将要读取的文件移动到python文件的同一文件夹下,然后您只需要使用文件名,而无需调用其他路径。