os.path關於路徑的方法整理
阿新 • • 發佈:2018-12-14
#encoding: utf8 import os #當前檔案的完整路徑名:C:\Python27\Scripts\djangotest\justtest.py print os.path.dirname(os.path.dirname(__file__))#獲取當前檔案所在目錄的上級目錄:C:/Python27/Scripts print os.path.dirname(__file__)#獲取當前檔案所在目錄:C:/Python27/Scripts/djangotest print os.getcwd()#獲取當前檔案目錄:C:\Python27\Scripts\djangotest print os.path.dirname(r'd:\workspace\R')#獲取指定目錄的上級目錄:d:\workspace #******************************獲取相對路徑******************************************************** print os.path.basename(__file__)#獲取當前檔名稱:justtest.py print os.path.basename(r'd:\workspace\R')#獲取指定目錄的相對路徑,即當前目錄名:R #******************************獲取絕對路徑******************************************************** print os.path.abspath(__file__)#獲取當前檔案的絕對路徑:C:\Python27\Scripts\djangotest\justtest.py print os.path.abspath(r'd:\workspace\R')#獲取指定目錄的絕對路徑:d:\workspace\R