在Python中,可以使用os.path.dirname()
函数来获取指定路径的父目录。
以下是一个代码示例:
import os
# 获取当前文件的绝对路径
current_path = os.path.abspath(__file__)
# 获取当前文件的父目录
parent_directory = os.path.dirname(current_path)
# 输出父目录的绝对路径
print(parent_directory)
这段代码首先使用os.path.abspath(__file__)
获取当前文件的绝对路径,然后使用os.path.dirname()
函数获取该绝对路径的父目录路径,并将其赋值给变量parent_directory
,最后将父目录的绝对路径打印出来。