os.path.dirname用法


语法:os.path.dirname(path)

功能:去掉文件名,返回目录

handle_path.py

print
(os.path.dirname(D:/Project/Delivery_System/utils/handle_path.py)) #结果 D:/Project/Delivery_System/utils

语法:os.path.dirname(__file__)

先了解__file__,表示了当前文件的path

 print(__file__)
# 测试结果
D:/Project/Delivery_System/utils/handle_path.py

可以了解到os.path.dirname((__file__)和os.path.dirname(“E:/Read_File/read_yaml.py”)是一个意思
再根据os.path.dirname(path)的用法,得出os.path.dirname((__file__)就是得到当前文件的绝对路径