python獲取當前檔案路徑以及父檔案路徑

2021-08-19 23:00:18 字數 1727 閱讀 7278

#當前檔案的路徑

pwd = os.getcwd()

#當前檔案的父路徑

father_path=os.path.abspath(os.path.dirname(pwd)+os.path.sep+".")

#當前檔案的前兩級目錄

grader_father=os.path.abspath(os.path.dirname(pwd)+os.path.sep+"..")

追加部分**例項

def testprtpwd(self):

print("獲取當前檔案路徑——" + os.path.realpath(__file__)) # 獲取當前檔案路徑

parent = os.path.dirname(os.path.realpath(__file__))

print("獲取其父目錄——" + parent) # 從當前檔案路徑中獲取目錄

garder = os.path.dirname(parent)

print("獲取父目錄的父目錄——" + garder)

print("獲取檔名" + os.path.basename(os.path.realpath(__file__))) # 獲取檔名

# 當前檔案的路徑

pwd = os.getcwd()

print("當前執行檔案路徑" + pwd)

# 當前檔案的父路徑

father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")

print("執行檔案父路徑" + father_path)

# 當前檔案的前兩級目錄

grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")

print("執行檔案父路徑的父路徑" + grader_father)

return garder

執行結果:

獲取當前檔案路徑——d:\svn\測試\autotest\functionalautomation\aonr_sxsj\auditdata\common\redconfig.py

獲取其父目錄——d:\svn\測試\autotest\functionalautomation\aonr_sxsj\auditdata\common

獲取父目錄的父目錄——d:\svn\測試\autotest\functionalautomation\aonr_sxsj\auditdata

獲取檔名redconfig.py

當前執行檔案路徑d:\svn\測試\autotest\functionalautomation\aonr_sxsj\auditdata\testsuite\rolemanagement

執行檔案父路徑d:\svn\測試\autotest\functionalautomation\aonr_sxsj\auditdata\testsuite

執行檔案父路徑的父路徑d:\svn\測試\autotest\functionalautomation\aonr_sxsj\auditdata

獲取當前檔案路徑

getcurrentdirectory只是返回當前程序的當前目錄,而並不是程序的映象檔案 exe 所在的目錄 getcurrentdirectory 適用於xp等系統,在wince上不能使用 getmodulefilename 適用於wince2.0以後 使用方法 下面的一段 主要是獲得當前程式的執...

獲取當前檔案路徑。

之前搞模組載入,苦於無法獲取當前檔案路徑,檔名 後來沒通過獲取當前檔案路徑,檔名,給搞定了。結果今天把這個問題也解決了,做個記錄 file zhus err.js varcursrc newfunction catch e console.log cursrc zhus err 以上編碼,只適用於f...

獲取當前檔案的路徑

一 獲取當前檔案的路徑 1.system.diagnostics.process.getcurrentprocess mainmodule.filename 獲取模組的完整路徑,包括檔名。2.system.environment.currentdirectory 獲取和設定當前目錄 該程序從中啟動的...