Python获取当前文件路径、上级路径

__file__

__file__:当前文件的位置,如:

print(__file__)                  #打印文件当前的位置

获取当前文件的绝对路径

file_dir=os.path.abspath(__file__)
print(file_dir)

获取当前文件的 所在路径

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

获取当前文件的 上级路径

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