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)
原文地址:https://www.cnblogs.com/xiazhenyu/p/15403126.html