python路径相关操作:os.path
Windows路径格式
import os
# 当前python文件位置:T:\ProgrammingPractice\python_path\test.py
# 给定的路径
path = r'D:\AAA\BBB\CCC\x.jpg' # path = 'D:\\AAA\\BBB\\CCC\\x.jpg'
# 获取路径的目录部分
dir = os.path.dirname(path)
# 获取最后一个目录名
last_dir = os.path.basename(dir)
# 打印结果
print(dir) # D:\AAA\BBB\CCC
print(last_dir) # CCC
print(os.path.basename(path)) # x.jpg
print(os.path.splitext(path)) # ('D:\\AAA\\BBB\\CCC\\x', '.jpg')
print(os.path.splitext(path)[0]) # D:\AAA\BBB\CCC\x
print(os.path.splitext(path)[1]) # .jpg
print(os.path.abspath(path)) # D:\AAA\BBB\CCC\x.jpg
print(os.path.abspath(dir)) # D:\AAA\BBB\CCC
print(os.path.join(os.path.abspath(dir), 'y.txt')) # D:\AAA\BBB\CCC\y.txt
print(os.path.splitdrive(path)) # ('D:', '\\AAA\\BBB\\CCC\\x.jpg')
print(os.path.splitdrive(path)[0]) # D:
Linux路径格式
绝对路径
import os
# 当前文件位置:T:\ProgrammingPractice\python_path\test.py
# 给定的路径
path = '/AAA/BBB/CCC/x.jpg'
# 获取路径的目录部分
dir = os.path.dirname(path)
# 获取最后一个目录名
last_dir = os.path.basename(dir)
# 打印结果
print(dir) # /AAA/BBB/CCC
print(last_dir) # CCC
print(os.path.basename(path)) # x.jpg
print(os.path.splitext(path)) # ('/AAA/BBB/CCC/x', '.jpg')
print(os.path.splitext(path)[0]) # /AAA/BBB/CCC/x
print(os.path.splitext(path)[1]) # .jpg
print(os.path.abspath(path)) # T:\AAA\BBB\CCC\x.jpg
print(os.path.abspath(dir)) # T:\AAA\BBB\CCC
print(os.path.join(os.path.abspath(dir), 'y.txt')) # T:\AAA\BBB\CCC\y.txt
print(os.path.splitdrive(path)) # ('', '/AAA/BBB/CCC/x.jpg')
当前目录相对路径
import os
# 当前文件位置:T:\ProgrammingPractice\python_path\test.py
# 给定的路径
path = './AAA/BBB/CCC/x.jpg'
# 获取路径的目录部分
dir = os.path.dirname(path)
# 获取最后一个目录名
last_dir = os.path.basename(dir)
# 打印结果
print(dir) # ./AAA/BBB/CCC
print(last_dir) # CCC
print(os.path.basename(path)) # x.jpg
print(os.path.splitext(path)) # ('./AAA/BBB/CCC/x', '.jpg')
print(os.path.splitext(path)[0]) # ./AAA/BBB/CCC/x
print(os.path.splitext(path)[1]) # .jpg
print(os.path.abspath(path)) # T:\ProgrammingPractice\python_path\AAA\BBB\CCC\x.jpg
print(os.path.abspath(dir)) # T:\ProgrammingPractice\python_path\AAA\BBB\CCC
print(os.path.join(os.path.abspath(dir), 'y.txt')) # T:\ProgrammingPractice\python_path\AAA\BBB\CCC\y.txt
print(os.path.splitdrive(path)) # ('', './AAA/BBB/CCC/x.jpg')
上层目录相对路径
import os
# 当前文件位置:T:\ProgrammingPractice\python_path\test.py
# 给定的路径
path = '../AAA/BBB/CCC/x.jpg'
# 获取路径的目录部分
dir = os.path.dirname(path)
# 获取最后一个目录名
last_dir = os.path.basename(dir)
# 打印结果
print(dir) # ../AAA/BBB/CCC
print(last_dir) # CCC
print(os.path.basename(path)) # x.jpg
print(os.path.splitext(path)) # ('../AAA/BBB/CCC/x', '.jpg')
print(os.path.splitext(path)[0]) # ../AAA/BBB/CCC/x
print(os.path.splitext(path)[1]) # .jpg
print(os.path.abspath(path)) # T:\ProgrammingPractice\AAA\BBB\CCC\x.jpg
print(os.path.abspath(dir)) # T:\ProgrammingPractice\AAA\BBB\CCC
print(os.path.join(os.path.abspath(dir), 'y.txt')) # T:\ProgrammingPractice\AAA\BBB\CCC\y.txt
print(os.path.splitdrive(path)) # ('', '../AAA/BBB/CCC/x.jpg')
很高兴本文对你有用(*^_^*),如需转载请记得标明出处哟(☆▽☆):
本文来自博客园,作者:香菜大魔法师,原文链接:https://www.cnblogs.com/corianderfiend/p/18133567