代码在PyCharm上运行,但不是Jupyter

时间:2017-11-11 08:17:12

标签: python excel pandas pycharm jupyter

我必须打开一个excel文件,我这样做:

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

在PyCharm(Python 2.7.8)上它运行得很好,但在Jupyter(Python 3)上,我总是出现这个错误:

FileNotFoundError: [Errno 2] No such file or directory

可能是什么原因?

2 个答案:

答案 0 :(得分:1)

如果您在根目录以外的位置调用jupyter notebook,则可能会发生这种情况。在这种情况下,jupyter可能无法访问该文件。

尝试转到D:并调用jupyter notebook然后重试此操作。另一种选择是使用以下方式获取笔记本的路径:

os.path.abspath("__file__")

然后设置数据集的相对路径。

编辑:

假设您要在包含笔记本的目录上方设置一个级别的路径。然后你会这样做:

foo = os.path.dirname(os.path.abspath("__file__"))
relative_path = os.path.join(foo, '..')

答案 1 :(得分:0)

按照本文how to change jupyter start folder?中的建议更改了Jupyter启动文件夹后,如果文件在此文件夹中,要加载它们,则无需编写路径。这就足够了:

xl_file = pd.ExcelFile('myFile.xls')