如何知道/更改Python shell中的当前目录?

我在Windows 7上使用Python 3.2。当我打开Python shell时,如何知道当前目录是什么以及如何将其更改为模块所在的另一个目录?


你可以使用os模块。

>>> import os
>>> os.getcwd()
'/home/user'
>>> os.chdir("/tmp/")
>>> os.getcwd()
'/tmp'

但是如果是关于查找其他模块的话:你可以设置一个名为PYTHONPATH的环境变量,在Linux下就好像

export PYTHONPATH=/path/to/my/library:$PYTHONPATH

然后,解释器也在这个地方搜索import ed模块。 我猜这个名字在Windows下是一样的,但不知道如何改变。

编辑

在Windows下:

set PYTHONPATH=%PYTHONPATH%;C:My_python_lib

(取自http://docs.python.org/using/windows.html)

编辑2

...甚至更好:使用virtualenvvirtualenv_wrapper ,这将允许您创建一个开发环境,您可以随意添加模块路径( add2virtualenv ),而不会污染您的安装或“正常”工作环境。

http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html


你要

import os
os.getcwd()
os.chdir('..')

>>> import os
>>> os.system('cd c:mydir')

实际上, os.system()可以执行windows命令提示可以执行的任何命令,而不仅仅是更改dir。

链接地址: http://www.djcxy.com/p/9283.html

上一篇: How to know/change current directory in Python shell?

下一篇: How to properly determine current script directory?