返回n个上级目录
import os
path = r'G:\2021.9-8000公里数据5\东肖新线上行二车道AK10\GPS_GE\20210714\S76A-123324\S76A-CiCO-10.600-标准格式-20210714123324-JZ'
print(path) # 原始路径
print(os.path.dirname(path)) # 返回上个目录
print(os.path.abspath(os.path.join(path, ".."))) # 返回上一个目录
print(os.path.abspath(os.path.join(path, "../.."))) # 返回上上个目录
print(os.path.abspath(os.path.join(path, "../../.."))) # 返回上上上个目录
print(os.path.abspath(os.path.join(path, "../../../.."))) # 返回上上上上个目录
# 以此类推,每增加一个上级目录就增加一个“..”中间用“/”隔开
打印输出:
G:\2021.9-8000公里数据5\东肖新线上行二车道AK10\GPS_GE\20210714\S76A-123324\S76A-CiCO-10.600-标准格式-20210714123324-JZ
G:\2021.9-8000公里数据5\东肖新线上行二车道AK10\GPS_GE\20210714\S76A-123324
G:\2021.9-8000公里数据5\东肖新线上行二车道AK10\GPS_GE\20210714\S76A-123324
G:\2021.9-8000公里数据5\东肖新线上行二车道AK10\GPS_GE\20210714
G:\2021.9-8000公里数据5\东肖新线上行二车道AK10\GPS_GE
G:\2021.9-8000公里数据5\东肖新线上行二车道AK10