How to know/change current directory in Python shell?

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


You can use the os module.

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

But if it's about finding other modules: You can set an environment variable called PYTHONPATH , under Linux would be like

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

Then, the interpreter searches also at this place for import ed modules. I guess the name would be the same under Windows, but don't know how to change.

edit

Under Windows:

set PYTHONPATH=%PYTHONPATH%;C:My_python_lib

(taken from http://docs.python.org/using/windows.html)

edit 2

... and even better: use virtualenv and virtualenv_wrapper , this will allow you to create a development environment where you can add module paths as you like ( add2virtualenv ) without polluting your installation or "normal" working environment.

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/9284.html

上一篇: 如何在Python中获取当前执行文件的路径?

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