os.path 常用的使用方法
1. __file__
→ 当前Python文件的路径;
2. os.path.abspath(__file__) 当前Python文件的相对路径;
3.. os.path.dirname(os.path.abspath(__file__) ) 当前Python文件的相对路径的父级目录;
一般常见的用法: currpath = os.path.dirname(os.path.abspath(__file__) )
modelpath = os.path.join(currpath,'model')
4. os.path.join(a, b, ...) 记住:它会自动加分隔符,不需要手动人为添加;
5. os.path.exists(path):判断路径是否存在
常见用法: 当写完一个modelpath 之后 ,需要保护判断:
if not os.path.exist(modelpath):
print('路径不存在')
6. os.path.getctime(path) 创建时间戳 ,如 currtime 1745477514.9281628