如何更改python中的默认工作目录 [英] How to change the default working directory in python
问题描述
我有一个关于直接在 python 上工作的默认值的快速问题.我目前正在使用 python 2.7.在这种情况下,默认直接工作的是 C:/Python27
.我想将此永久更改为另一个目录.即使我们在shell上写下以下内容,默认工作目录也会自动回到原来的C:/Python27
.
I have a quick question about the default working directly on python.
I am currently using the python 2.7. In this case the default working directly is the C:/Python27
.
I want to change this permanently to another directory.
Even if we write down following on shell, the default working directory will go back to the original C:/Python27
automatically.
import os
os.chdir('a path')
有谁知道如何将默认目录永久设置为关闭shell后保留目录的路径"?这与如何通过函数临时更改工作目录不同.
非常感谢您的帮助!!
Does any one know how to set up default directory permanently to "the path" which keeps the directory after the closing the shell?
This is a different question from how to change working directory just by the function temporarily.
Thank you so much for your help!!
推荐答案
python
目录的工作目录是它启动的目录.如果从控制台(cmd.exe
)我做 cd/some/dir
然后启动 python,它的工作目录将是 /some/dir
.
The working directory of the python
directory is the directory from which it was started. If from a console (cmd.exe
) I do cd /some/dir
and then start python, its working directory will be /some/dir
.
如果您希望在给定目录中启动交互式控制台,您有一些选择:
If you want an interactive console started at a given directory you have some options:
- 使用快捷方式,您可以在属性选项卡中将其
Start in
字段更改为指向您的目标目录. - 改用
IPython
,并添加一个 启动脚本 到默认配置文件,以便它始终从目标目录启动.IPython 是一种增强型交互工具,具有许多有用的功能.
- Using a shortcut, you can change its
Start in
field, in the properties tab, to point to your target directory. - Use
IPython
instead, and add a startup script to the default profile so it always start at the target directory. IPython is an enhanced interactive with lots of useful features.
如果您从脚本运行并想切换到存储脚本的文件夹,您可以使用 os.cd(os.path.dirname(__file__))
.
If you are running from a script and want to switch to the folder where you script is stored, you could use os.cd(os.path.dirname(__file__))
.
这篇关于如何更改python中的默认工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!