import os
# 获取当前目录
print(os.getcwd())
# 获取上级目录
print(os.path.abspath(os.path.join(os.getcwd(), "..")))
# 获取上上级目录
print(os.path.abspath(os.path.join(os.getcwd(), "../..")))
# 获取上上上级目录
print(os.path.abspath(os.path.join(os.getcwd(), "../../..")))
运行结果:
C:\Users\86177\Desktop\temporary
C:\Users\86177\Desktop
C:\Users\86177
C:\Users