python获取文件的绝对路径

python获取文件的绝对路径

import os


def load_file():
    # 获取当前文件路径
    current_path = os.path.abspath(__file__)
    # 获取当前文件的父目录
    father_path = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".")
    # config.ini文件路径,获取当前目录的父目录的父目录与congig.ini拼接
    config_file_path = os.path.join(os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".."), 'config.ini')
    print('当前目录:' + current_path)
    print('当前父目录:' + father_path)
    print('config.ini路径:' + config_file_path)


load_file()

import os

path1=os.path.abspath(’.’) # 表示当前所处的文件夹的绝对路径
print(path1)
path2=os.path.abspath(’…’) # 表示当前所处的文件夹上一级文件夹的绝对路径
print(path2)
原文地址:https://www.cnblogs.com/shierlou-123/p/14311619.html