sample code
import configparser
import os
conf_directory = "conf"
config_file_path = os.path.join('.', conf_directory + '/sysConn.cfg').replace('\\', '/')
config = configparser.ConfigParser()
config.read(config_file_path)
# 讀取
# str type, ex: config['sysConn']['conn']
conn = config['sysConn']['conn']
print(conn, type(conn))
# int type, ex: config['sysConn'].getint('conn')
conn = config['sysConn'].getint('conn')
print(conn, type(conn))
# 寫入
# 寫入時只允許 str type
config['sysConn']['conn'] = str(0)
# 寫入後再印出驗證是否有正確寫入
new_conn = config['sysConn']['conn']
print(new_conn, type(new_conn))
# 寫入更新檔案 (寫入檔案後實際打開 sysConn.cfg 查看是否被改)
with open(config_file_path, 'w') as f:
config.write(f)
Python 寫入 ini 設定檔
Python内置库:configparser(INI格式配置文件解析)
實作相關
因不支援副檔名為xmls的檔案,故須透過 offiece 轉成xls,也不能直接從檔名改,否則一樣不支援 Ref. Localizable.strings2Excel
Jun 1, 2022Ref. Python内置库:configparser(INI格式配置文件解析)
Oct 25, 2021Ref. 【python的異常處理】異常的捕捉
Jul 22, 2021調整了範例,讓 timer t 可在任意位置取消 timer 的運作,指令 t.cancel() import threading # global t t = None def set_interval(func, sec): global t
Jul 21, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up