獲取當前文件路徑、上層目錄、設置其他目錄方法
import os print("===獲取當前文件目錄===") # 當前腳本工作的目錄路徑 print(os.getcwd()) # os.path.abspath()獲得絕對路徑 print(os.path.abspath(os.path.dirname(__file__))) print("=== 獲取當前文件上層目錄 ===") print(os.path.abspath(os.path.dirname(os.path.dirname(__file__)))) print(os.path.abspath(os.path.dirname(os.getcwd()))) print(os.path.abspath(os.path.join(os.getcwd(), ".."))) print(os.path.dirname(os.getcwd())) # os.path.join()連接目錄名與文件或目錄 print("==== 設置路徑為當前文件上層目錄的test_case文件夾====") path = os.path.join(os.path.dirname(os.getcwd()), "test_case") print(path)