python项目中获取当前文件所在目录

import os
class GetDir:
def __init__(self):
pass
@staticmethod
def get_BASE_DIR():
BASE_DIR = os.path.dirname(__file__)#当前文件所在目录
# print("path:" + BASE_DIR)
# BASE_DIR = BASE_DIR + "/"
print("BASE_DIR:" + BASE_DIR)
return BASE_DIR

@staticmethod
def get_BASE_DIR2(dir):
BASE_DIR = os.path.dirname(os.path.dirname(__file__))#当前文件所在目录的上一层目录
# print("path:" + BASE_DIR)
BASE_DIR = BASE_DIR + "/" + dir + "/"
print("BASE_DIR:" + BASE_DIR)
return BASE_DIR
if __name__ == '__main__':
GetDir.get_BASE_DIR()
dir="adddir"
GetDir.get_BASE_DIR2(dir)
posted @ 2021-10-13 17:20  sunny.boy  阅读(1135)  评论(0编辑  收藏  举报