先要装载 os模块:
import os
print os.getcwd()
或者
print os.path.abspath(os.curdir)
print os.path.abspath('.')
就可以了。
( . 代表当前的路径,.. 代表当前路径的上一级路径。这在UNIX和Windows系统意义是类似的。
例如:
以Winodws系统为例:
当前路径是F:Temp, 则 . 的绝对路径就是F:Temp
.. 的绝对路径就是F:
)
要获得上级目录的路径也很简单,print os.path.abspath('..')就可以了。
要改变当前路径,os.chdir(path) 就可以了 path里填要改变到的目录,例如os.chdir('D:Program Files')
这样大部分的文件操作现在是相对于D:Program Files 来了,例如fobj = open('Hello.txt'),实际会打开D:Program FilesHello.txt文件。