os.path.dirname(os.path.dirname(__file__)) 与os.path.dirname(os.path.abspath(__file__))

os.path.dirname(file)

返回脚本的路径,即文件路径中所在的目录(不包含文件名)
1.可以输入文件名或文件路径进行
使用格式:
1.输入文件的全路径才会返回文件的目录(即不包括该文件)
比如 os.path.dirname(‘D:\test\t.py’)
返回D:\test
2.只输入文件的名字,则返回为空。
比如 os.path.dirname(‘t.py’)
返回为空
3.os.path.dirname(file)
返回当前文件的目录,后面有结果截图。

这不是总结的重点。
题目中的那个组合才是重点:

os.path.abspath(file)

返回文件的绝对路径,可以看到下面图片的效果,当然这也不重点。
os.path.dirname(os.path.dirname(__file__)) 与os.path.dirname(os.path.abspath(__file__))

os.path.dirname(os.path.dirname(file)) 与os.path.dirname(os.path.abspath(file))

os.path.dirname(os.path.dirname(file))这个组合有什么作用呢?
先看一下 os.path.dirname(file)
它会返回当前文件的目录(不包含当前文件)
os.path.dirname(os.path.dirname(__file__)) 与os.path.dirname(os.path.abspath(__file__))
os.path.dirname(os.path.dirname(file))os.path.dirname(os.path.dirname(__file__)) 与os.path.dirname(os.path.abspath(__file__))
因此os.path.dirname(os.path.dirname(file)) 就返回当前文件的目录的上一级目录
那么os.path.dirname(os.path.abspath(file))呢?
os.path.dirname(os.path.dirname(__file__)) 与os.path.dirname(os.path.abspath(__file__))
os.path.dirname(os.path.abspath(file)) 返回当前文件的目录

我们经常会在写项目的时候遇到这两个组合,希望可以把这两个组合区分开来,在合适的地方运用。
今天的总结就到这,如果有任何错误,欢迎指正,非常感谢