python中os模块获取路径的几种方式

一、代码

import os

BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
print(BASE_DIR)


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(BASE_DIR)

BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
print(BASE_DIR)

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
print(BASE_DIR)
#结果
D:\PycharmProjects\RPA
D:\PycharmProjects\RPA
D:\PycharmProjects
D:\PycharmProjects

 

posted @ 2019-09-18 10:01  Maple_feng  阅读(4556)  评论(0编辑  收藏  举报