python獲取當前文件路徑以及父文件路徑的方法
更新時間:2019年07月10日 10:07:32 作者:lxting21
今天小編就為大家分享一篇python獲取當前文件路徑以及父文件路徑的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
#當前文件的路徑 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獲取當前文件路徑以及父文件路徑的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
解決Python3 控制臺輸出InsecureRequestWarning問題
這篇文章主要介紹了解決Python3 控制臺輸出InsecureRequestWarning的問題 ,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07window11系統(tǒng)下Python3.11安裝numpy庫超詳細教程
這篇文章主要給大家介紹了關于window11系統(tǒng)下Python3.11安裝numpy庫的相關資料,NumPy是Python的第三方擴展包,但它并沒有包含在Python標準庫中,因此您需要單獨安裝它,需要的朋友可以參考下2023-12-12Django使用模板后無法找到靜態(tài)資源文件問題解決
這篇文章主要介紹了Django使用模板后無法找到靜態(tài)資源文件問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-07-07Python?Concurrent?Futures解鎖并行化編程的魔法示例
Python的concurrent.futures模塊為并行化編程提供了強大的工具,使得開發(fā)者能夠輕松地利用多核心和異步執(zhí)行的能力,本文將深入探討concurrent.futures的各個方面,從基礎概念到高級用法,為讀者提供全面的了解和實用的示例代碼2023-12-12