当前位置: 首页 > 图灵资讯 > 行业资讯> 3分钟学会python获取当前运行文件路径的方法

3分钟学会python获取当前运行文件路径的方法

来源:图灵python
时间: 2025-03-04 22:16:27

以下是python获取当前运行文件路径的方法

通过 __file__ 获取

os.path.dirname(os.path.realpath(__file__))

通过 getcwd 获取当前路径

os.getcwd()

其它相关路径

#当前文件路径
print(os.path.realpath(__file__))
#当前文件所在的目录,即父路径
print(os.path.split(os.path.realpath(__file_)[0])
#在父亲的路径下找到其他文件,即同级别的其他文件
print(os.path.join(proDir,"config.ini"))