>>> 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
如果您 import os 可以使用 os.getcwd 获取当前工作目录,并且可以使用 os.chdir 更改目录
# Import the os package
import os
# Confirm the current working directory
os.getcwd()
# Use '\\' while changing the directory
os.chdir("C:\\user\\foldername")
6 回答
您可以使用
os
模块 .但是,如果它是关于寻找其他模块:您可以设置一个名为
PYTHONPATH
的环境变量,在Linux下就像然后,解释器也在这个地方搜索
import
ed模块 . 我想在Windows下名称会相同,但不知道如何更改 .edit
在Windows下:
(摘自http://docs.python.org/using/windows.html)
edit 2
......甚至更好:使用
virtualenv
和virtualenv_wrapper
,这将允许您创建一个开发环境,您可以根据需要添加模块路径(add2virtualenv
),而不会污染您的安装或"normal"工作环境 .http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html
你要
实际上,
os.system()
可以执行windows命令提示符可以执行的任何命令,而不仅仅是更改dir .更改当前目录不是处理在Python中查找模块的方法 .
相反,请参阅The Module Search Path的文档,了解Python如何找到要导入的模块 .
以下是Standard Modules部分的相关位:
回答关于获取和设置当前目录的原始问题:
如果您
import os
可以使用os.getcwd
获取当前工作目录,并且可以使用os.chdir
更改目录在python中更改当前工作目录的最简单方法是使用'os'包 . 下面是一个Windows计算机的例子: