python讀寫配置檔案

2021-08-09 13:10:56 字數 847 閱讀 3295

本文主要介紹在python中讀寫配置檔案。

python主要使用configparser模組來進行配置檔案的操作。

import configparser

#讀操作

conf = configparser.configparser()

conf.read('config.ini') #檔名

#讀取指定的section和name項

user = conf.get('global', 'user')

password = conf.get('global', 'password')

#獲取所有的section

sections = conf.sections()

----------

#寫操作

# 更新指定section, option的值

conf.set("section2", "port", "8081")

# 寫入指定section, 增加新option的值

conf.set("section2", "ieport", "80")

# 新增新的 section

conf.add_section("new_section")

conf.set("new_section", "new_option", "")

# 寫回配置檔案

conf.write(open("c:\\test.conf","w"))

config.ini配置檔案內容如下:

[global]

user = 'root'

password = 'test'

Python之配置檔案讀寫

configparser模組 一 建立配置檔案 在d盤建立乙個配置檔案,名字為 test.ini 內容如下 baseconf host 127.0.0.1 port 3306 user root password root db name gloryroad test ip 127.0.0.1 in...

python配置檔案讀寫(ConfigParse)

ini檔案格式 section0 key0 value0 key1 value1 section1 key2 value2 key3 value示例 config num 40column 8create size x 400create size y 300 color 深藍 dark blue ...

讀寫配置檔案

windows作業系統專門為此提供了6個api函式來對配置設定檔案進行讀 寫 getprivateprofileint 從私有初始化檔案獲取整型數值 getprivateprofilestring 從私有初始化檔案獲取字串型值 getprofileint 從win.ini 獲取整數值 getprof...