要获得上级目录的路径也很简单,print os.path.abspath('..')就可以了。
要改变当前路径,os.chdir(path) 就可以了 path里填要改变到的目录,例如os.chdir('D:\Program Files')
这样大部分的文件操作现在是相对于D:\Program Files 来了,例如fobj = open('Hello.txt'),实际会打开D:\Program Files\Hello.txt文件。
import osprint os.getcwd()