Python获取当前路径下的配置文件

Python获取当前路径下的配置文件

有的时候想读取当前目录下的一个配置文件。其采用的办法是:html

import os

# 获取当前路径
curr_dir = os.path.dirname(os.path.realpath(__file__))

# 合成完整路径
config_file = curr_dir + os.sep + "my.conf"

其中__file__是指当前执行的python文件。python

os.path.realpath() 返回的是真实地址 os.path.abspath() 返回的是软链接地址spa

参考:code

相关文章
相关标签/搜索