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)
print(os.path.dirname(os.path.abspath("__file__"))) print(os.path.pardir) print(os.path.join(os.path.dirname("__file__"), os.path.pardir)) print(os.path.abspath(os.path.join(os.path.dirname("__file__"),os.path.pardir))) print(os.path.abspath(os.path.join(os.path.dirname("__file__"),os.path.pardir,os.path.pardir)))
curPath = os.path.abspath(os.path.join(os.path.dirname("__file__"), os.path.pardir, os.path.pardir)) print(curPath) yamlPath = os.path.join(curPath, "common", "token.yaml") print(yamlPath) f = open(yamlPath, 'r', encoding='utf-8') cfg = f.read() print(type(cfg)) # 讀出來是字符串 print(cfg) d = yaml.load(cfg) # 用load方法轉字典 print(d)
import os curpath = os.path.realpath(__file__) # 獲取當前文件絕對路徑 print(curpath) dirpath = os.path.dirname(curpath) # 獲取當前文件的文件夾路徑 print(dirpath) casespath = os.path.join(dirpath, "cases") # 拼接文件路徑 print(casespath) report = os.path.join(dirpath, "report", "result.html") # 拼接文件夾路徑 print(report)