Python獲取當前路徑下的配置文件
有的時候想讀取當前目錄下的一個配置文件。其采用的辦法是:
import os
# 獲取當前路徑
curr_dir = os.path.dirname(os.path.realpath(__file__))
# 合成完整路徑
config_file = curr_dir + os.sep + "my.conf"
其中__file__
是指當前執行的python文件。
os.path.realpath()
返回的是真實地址
os.path.abspath()
返回的是軟連接地址
參考: