如何在Python Shell中知道/更改当前目录?

问题:如何在Python Shell中知道/更改当前目录?

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

I am using Python 3.2 on Windows 7. When I open the Python shell, how can I know what the current directory is and how can I change it to another directory where my modules are?


回答 0

您可以使用该os模块。

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

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

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

然后,解释器也在此位置搜索imported模块。我想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

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 imported 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


回答 1

你要

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

you want

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

回答 2

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

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

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

In fact, os.system() can execute any command that windows command prompt can execute, not just change dir.


回答 3

更改当前目录不是处理Python中的模块的方法。

相反,请参阅有关模块搜索路径的文档获取Python如何找到要导入的模块的信息。

以下是“ 标准模块”部分的相关内容:

变量sys.path是一个字符串列表,该字符串确定解释器对模块的搜索路径。它初始化为从环境变量PYTHONPATH提取的默认路径,或者如果未设置PYTHONPATH则从内置的默认路径初始化。您可以使用标准列表操作对其进行修改:

>>> import sys
>>> sys.path.append('/ufs/guido/lib/python')

在回答有关获取和设置当前目录的原始问题时:

>>> help(os.getcwd)

getcwd(...)
    getcwd() -> path

    Return a string representing the current working directory.

>>> help(os.chdir)

chdir(...)
    chdir(path)

    Change the current working directory to the specified path.

Changing the current directory is not the way to deal with finding modules in Python.

Rather, see the docs for The Module Search Path for how Python finds which module to import.

Here is a relevant bit from Standard Modules section:

The variable sys.path is a list of strings that determines the interpreter’s search path for modules. It is initialized to a default path taken from the environment variable PYTHONPATH, or from a built-in default if PYTHONPATH is not set. You can modify it using standard list operations:

>>> import sys
>>> sys.path.append('/ufs/guido/lib/python')

In answer your original question about getting and setting the current directory:

>>> help(os.getcwd)

getcwd(...)
    getcwd() -> path

    Return a string representing the current working directory.

>>> help(os.chdir)

chdir(...)
    chdir(path)

    Change the current working directory to the specified path.

回答 4

在python中更改当前工作目录的最简单方法是使用“ os”包。下面是Windows计算机的示例:

# Import the os package
import os

# Confirm the current working directory 
os.getcwd()

# Use '\\' while changing the directory 
os.chdir("C:\\user\\foldername")

The easiest way to change the current working directory in python is using the ‘os’ package. Below there is an example for windows computer:

# Import the os package
import os

# Confirm the current working directory 
os.getcwd()

# Use '\\' while changing the directory 
os.chdir("C:\\user\\foldername")

回答 5

如果您import os可以os.getcwd用来获取当前的工作目录,并且可以os.chdir用来更改目录

If you import os you can use os.getcwd to get the current working directory, and you can use os.chdir to change your directory


回答 6

您可以尝试以下方法:

import os

current_dir = os.path.dirname(os.path.abspath(__file__))   # Can also use os.getcwd()
print(current_dir)                                         # prints(say)- D:\abc\def\ghi\jkl\mno"
new_dir = os.chdir('..\\..\\..\\')                         
print(new_dir)                                             # prints "D:\abc\def\ghi"

You can try this:

import os

current_dir = os.path.dirname(os.path.abspath(__file__))   # Can also use os.getcwd()
print(current_dir)                                         # prints(say)- D:\abc\def\ghi\jkl\mno"
new_dir = os.chdir('..\\..\\..\\')                         
print(new_dir)                                             # prints "D:\abc\def\ghi"