#當前文件的路徑 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
以上這篇python獲取當前文件路徑以及父文件路徑的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
