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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM