Python 如何在 Jupyter Notebook 中更改工作目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35664972/
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 change working directory in Jupyter Notebook?
提问by Y. Gao
I couldn't find a place for me to change the working directory in Jupyter Notebook, so I couldn't use the pd.read_csv method to read in a specific csv document.
我在 Jupyter Notebook 中找不到可以更改工作目录的位置,因此无法使用 pd.read_csv 方法读取特定的 csv 文档。
Is there any way to make it? FYI, I'm using Python3.5.1 currently.
有什么办法可以做到吗?仅供参考,我目前使用的是 Python3.5.1。
Thanks!
谢谢!
采纳答案by Rob?
Running os.chdir(NEW_PATH)
will change the working directory.
运行os.chdir(NEW_PATH)
将更改工作目录。
import os
os.getcwd()
Out[2]:
'/tmp'
In [3]:
os.chdir('/')
In [4]:
os.getcwd()
Out[4]:
'/'
In [ ]:
回答by ManojK
You may use jupyter magic command as below
您可以使用 jupyter 魔术命令,如下所示
%cd "C:\abc\xyz\"
回答by George Petropoulos
First you need to create the config file, using cmd:
jupyter notebook --generate-config
Then, search for C:\Users\your_username\.jupyter folder (Search for that folder), and right click edit the jupyter_notebook_config.py.
首先,您需要使用cmd创建配置文件:
jupyter notebook --generate-config
然后,搜索 C:\Users\your_username\.jupyter 文件夹(搜索该文件夹),然后右键单击编辑jupyter_notebook_config.py。
Then, Ctrl+F: #c.NotebookApp.notebook_dir =''. Note that the quotes are single quotes.Select your directory you want to have as home for your jupyter, and copy it with Ctrl+C, for example: C:\Users\username\Python Projects.
然后,Ctrl+F: #c.NotebookApp.notebook_dir =''。请注意,引号是单引号。选择您想要作为 jupyter 主目录的目录,然后使用Ctrl+复制它C,例如:C:\Users\username\Python Projects。
Then on that line, paste it like this : c.NotebookApp.notebook_dir = 'C:\\Users\\username\\Python Projects'
然后在该行上,像这样粘贴:c.NotebookApp.notebook_dir = 'C:\\Users\\username\\Python Projects'
Make sure to remove #, as it is as comment.
确保删除#,因为它是注释。
Make sure to double slash \\on each name of your path. Ctrl+Sto savethe config.py file !!!
确保在路径的每个名称上使用双斜线\\。 Ctrl+S保存config.py 文件!!!
Go back to your cmdand run jupyter notebook.
It should be in your directory of choice. Test it by making a folder and watch your directory from your computer.
回到你的cmd并运行jupyter notebook.
它应该在你选择的目录中。通过创建一个文件夹来测试它并从您的计算机上查看您的目录。
回答by Muhamed Al Khalil
Jupyter under the WinPythonenvironment has a batch file in the scripts
folder called:
WinPython环境下的jupyter在scripts
文件夹中有一个批处理文件,名为:
make_working_directory_be_not_winpython.bat
You need to edit the following line in it:
您需要在其中编辑以下行:
echo WINPYWORKDIR = %%HOMEDRIVE%%%%HOMEPATH%%\Documents\WinPython%%WINPYVER%%\Notebooks>>"%winpython_ini%"
replacing the Documents\WinPython%%WINPYVER%%\Notebooks
part with your folder address.
用Documents\WinPython%%WINPYVER%%\Notebooks
您的文件夹地址替换该部分。
Notice that the %%HOMEDRIVE%%%%HOMEPATH%%\
part will identify the root and user folders (i.e. C:\Users\your_name\
) which will allow you to point different WinPython installations on separate computers to the same cloud storage folder (e.g. OneDrive), accessing and working with the same files from different machines. I find that very useful.
请注意,该%%HOMEDRIVE%%%%HOMEPATH%%\
部分将标识根文件夹和用户文件夹(即C:\Users\your_name\
),这将允许您将不同计算机上的不同 WinPython 安装指向同一个云存储文件夹(例如 OneDrive),从而访问和使用来自不同计算机的相同文件。我觉得这很有用。
回答by Jason Lee
on Jupyter notebook, try this:
在 Jupyter 笔记本上,试试这个:
pwd #this shows the current directory
if this is not the directory you like and you would like to change, try this:
如果这不是您喜欢的目录并且您想更改,请尝试以下操作:
import os
os.chdir ('THIS SHOULD BE YOUR DESIRED DIRECTORY')
Then try pwd again to see if the directory is what you want.
然后再次尝试 pwd 以查看目录是否是您想要的。
It works for me.
这个对我有用。
回答by chethan
it is similar to jason lee as he mentioned earlier:
它类似于前面提到的 jason lee:
in Jupyter notebook, you can access the current working directoryby
在Jupyter笔记本电脑,您可以访问当前的工作目录由
pwd()
or by import OS from library and running os.getcwd()
或者通过从库中导入操作系统并运行 os.getcwd()
i.e. for example
即例如
In[ ]: import os
os.getcwd( )
out[ ]: :c\users\admin\Desktop\python
(#This is my working directory)
Changing Working Directory
更改工作目录
For changing the Working Directory (much more similar to current W.d just you need to change from os.getcwd()
to os.chdir('desired location')
更改工作目录(与当前的 Wd 更相似,只是您需要更改os.getcwd()
为os.chdir('desired location')
In[ ]: import os
os.chdir('c:user/chethan/Desktop') (#This is where i want to update my w.d,
like that choose your desired location)
out[ ]: 'c:user\chethan\Desktop'
回答by Wow
I have did it on windows machine. Detail mentioned below 1. From windows start menu open “Anaconda Promptenter image description here2. Find .jupyter folder file path .In command prompt just type enter image description hereor enter image description hereto find the .jupyter path
我在windows机器上做过。下面提到的细节 1. 从 windows 开始菜单打开“Anaconda Prompt enter image description here2. 找到 .jupyter 文件夹文件路径。在命令提示符中 输入 enter image description here或enter image description here以找到 .jupyter 路径
- After find the .jupyter folder, check there has “jupyter_notebook_config” file or not. If it is not there then run below command enter image description hereAfter run the command it will create "jupyter_notebook_config.py"
- 找到 .jupyter 文件夹后,检查是否有“jupyter_notebook_config”文件。如果它不存在然后运行下面的命令 在此处输入图像描述运行该命令后它将创建“jupyter_notebook_config.py”
if do not have administrator permission then Some time you could not find .jupyter folder . Still you can open config file from any of the text editor
如果没有管理员权限,那么有时您找不到 .jupyter 文件夹。您仍然可以从任何文本编辑器打开配置文件
- Open “jupyter_notebook_config.py” file from the the “.jypyter” folder.
- After open the file need to update the directory is use for notebooks and kernel. There are so many line in config file so find “#c.NotebookApp.notebook_dir” and update the path enter image description hereAfter: enter image description hereSave the file
- Now try to create or read some file from the location you set
回答by Rishabh mishra
Open jupyter notebook click upper right corner new and select terminal then type cd + your desired working path and press enter this will change your dir. It worked for me
打开 jupyter notebook 单击右上角的 new 并选择终端,然后键入 cd + 您想要的工作路径并按回车键,这将更改您的目录。它对我有用
回答by Wanderational
It's simple, every time you open Jupyter Notebook and you are in your current work directory, open the Terminal in the near top right corner position where create new Python file in. The terminal in Jupyter will appear in the new tab.
Type command cd <your new work directory>
and enter, and then type Jupyter Notebook
in that terminal, a new Jupyter Notebook will appear in the new tab with your new work directory.
很简单,每次打开 Jupyter Notebook 并且您在当前工作目录中时,在创建新 Python 文件的右上角位置打开终端。 Jupyter 中的终端将出现在新选项卡中。键入 commandcd <your new work directory>
并回车,然后Jupyter Notebook
在该终端中键入,一个新的 Jupyter Notebook 将出现在带有新工作目录的新选项卡中。