import os path1=os.path.abspath(__file__) print(path1)#當前文件的絕對路徑 path2=os.path.dirname(os.path.abspath(__file__)) print(path2)#當前文件的上一層目錄的絕對路徑 path3=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) print(path3)#當前文件的上上層目錄的絕對路徑 curpath = os.path.dirname(os.path.realpath(__file__)) # 當前文件夾的路徑
其中
語法:os.path.dirname(path)
功能:去掉文件名,返回目錄
__file__ 為內置屬性,表示當前文件的path
os.path.dirname((__file__) :指的是,得到當前文件的絕對路徑,是去掉腳本的文件名,只返回目錄。
os.path.dirname(os,path.realname(__file__)):指的是,獲得你剛才所引用的模塊所在的絕對路徑