os 相对路径与绝对路径

import os
path1=os.path.dirname(__file__)
path=os.path.dirname('/Users/mac126/Desktop/openstudy/images/girl.jpg')
path2=os.path.join(path1,)
print(path1,path)

r=os.path.isabs('/Users/mac126/Desktop/openstudy/images/girl.jpg')#判断是否---绝对路径

r2=os.path.isabs('../images/girl.jpg')#---相对路径  ..表示当前文件的上一级
r3=os.path.isabs('/images/girl.jpg')# 找当前文件同级别的images文件里面的girl.jpg


print('---->',r) #True
print('---->',r2) #False

#获取路径 :directory 目录 文件夹
#当前文件所在文件夹
path=os.path.dirname(__file__)
print(path)
#/Users/mac126/Desktop/openstudy

#通过相对路径,获取绝对路径

path=os.path.abspath('../images/girl.jpg')
print(path)

#/Users/mac126/Desktop/images/girl.jpg

#获取当前文件的 绝对路径

path=os.path.abspath(__file__)
print(path)
#/Users/mac126/Desktop/openstudy/路径.py

path=os.getcwd() #类似于 os.path.dirname(__file__)
print(path)
#/Users/mac126/Desktop/openstudy
原文地址:https://www.cnblogs.com/liangliangzz/p/11714528.html