導航:首頁 > 文件管理 > python配置文件創建

python配置文件創建

發布時間:2025-09-20 16:19:08

① 如何創建python的cfg配置文件

讀取config中info段中的name變數值.最後講講如何設置值.使用set(段名,變數名,值) 來設置變數.config.set(''info'',''age'',''21'') 表示把info段中age變數設置為21. 就這么簡單.

② python3 如何創建一個.ini的配置文件。

1、說明:
python3使用configparser模塊來處理ini配置文件。
2、代碼示例:
需要生成conf.ini配置文件如下:
[config]
v1
=
100
v2
=
abc
v3
=
true
v4
=
123.45
python代碼:
import
configparser
#
載入現有配置文件
conf
=
configparser.ConfigParser()
#
寫入配置文件
conf.add_section('config')
#添加section
#
添加值
conf.set('config',
'v1',
'100')
conf.set('config',
'v2',
'abc')
conf.set('config',
'v3',
'true')
conf.set('config',
'v4',
'123.45')
#
寫入文件
with
open('conf.ini',
'w')
as
fw:

conf.write(fw)
#
讀取配置信息
v1
=
conf.getint('config',
'v1')
v2
=
conf.get('config',
'v2')
v3
=
conf.getboolean('config',
'v3')
v4
=
conf.getfloat('config',
'v4')
print('v1:',
v1)
print('v2:',
v2)
print('v3:',
v3)
print('v4:',
v4)
打開conf.ini文件檢查內容
3、模塊常用函數:
1)讀取配置文件
read(filename)
直接讀取ini文件內容
sections()
得到所有的section,並以列表的形式返回
options(section)
得到該section的所有option
items(section)
得到該section的所有鍵值對
get(section,option)
得到section中option的值,返回為string類型
getint(section,option)
得到section中option的值,返回為int類型,還有相應的getboolean()和getfloat()
函數。
2)寫入配置文件
add_section(section)
添加一個新的section
set(
section,
option,
value)
對section中的option進行設置,需要調用write將內容寫入配置文件。

閱讀全文

與python配置文件創建相關的資料

熱點內容
投標文件公章蓋在哪個位置 瀏覽:910
bf是什麼文件 瀏覽:485
蘋果手機怎麼把視頻音樂轉為文件 瀏覽:537
那些人從小就敲代碼的 瀏覽:277
linux查看so文件 瀏覽:234
如何給ip埠發送數據 瀏覽:214
合格的文件名 瀏覽:396
政府如何打理網路犯罪建議 瀏覽:820
銳起伺服器網路設置 瀏覽:711
薄荷app老是請求超時 瀏覽:498
ashx更新資料庫 瀏覽:785
如何判斷自己編程水平 瀏覽:425
學數控編程要什麼電腦 瀏覽:646
wordpress製作評論 瀏覽:825
python配置文件創建 瀏覽:400
蘋果手環樂動力 瀏覽:365
擴展名frm的文件表示 瀏覽:352
ai教程電子書下載 瀏覽:384
驅動人生2014單文件 瀏覽:165
非會員企業升級10586 瀏覽:261

友情鏈接