python 获取当前文件夹路径及父级目录的几种方法

时间:2024-03-10 22:51:12

获取当前文件夹路径及父级目录:

import os
current_dir = os.path.abspath(os.path.dirname(__file__))
print(current_dir) #F:\project\pritice

current_dir1 = os.path.dirname(__file__)
print(current_dir1) #F:/project/pritice

parent_path = os.path.dirname(current_dir1)
print(parent_path) #F:/project

parent_path1  = os.path.dirname(parent_path)
print(parent_path1) #F:/

parent_path2 = os.path.dirname(current_dir)
print(parent_path2) #F:\project