File size: 851 Bytes
c45dff0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | """
为整个工程提供统一的绝对路径
"""
import os
def get_project_root() -> str:
"""
获取工程所在的根目录
:return: 字符串根目录
"""
# 当前文件的绝对路径
current_file = os.path.abspath(__file__)
# 获取工程的根目录,先获取文件所在的文件夹绝对路径
current_dir = os.path.dirname(current_file)
# 获取工程根目录
project_root = os.path.dirname(current_dir)
return project_root
def get_abs_path(relative_path: str) -> str:
"""
传递相对路径,得到绝对路径
:param relative_path: 相对领
:return: 绝对路径
"""
project_root = get_project_root()
return os.path.join(project_root, relative_path)
if __name__ == '__main__':
print(get_abs_path("config/config.txt"))
|