(1)os.path.dirname
语法:os.path.dirname(path)
功能:去掉文件名,返回目录
print(os.path.dirname("E:/File/read.py"))
#结果:
E:/File
(2)__file__
print(__file__)
#结果
E:/File/read.py
可以看出__file__表示了当前文件的path
os.path.dirname((__file__)和os.path.dirname(“E:/File/read.py”)是一个意思
得出os.path.dirname((__file__)就是得到当前文件的绝对路径
(3)os.path.abspath(__file__)
一、os.path.abspath()
取指定文件或目录的绝对路径(完整路径)
os.path.abspath(path) 返回path的绝对路径。
注意,不会去判断path路径是否存在!
参考:(5条消息) 【python初级】os.path.abspath(path)返回绝对路径_jn10010537的博客-CSDN博客_os.path.abspath
二、os.path.dirname(__file__)
返回脚本的绝对路径
import os
print(os.path.abspath(__file__))
D:\Python\PycharmProjects\pythonProject\crawl\os.path.测试.py