java中的接口是类吗
453
2022-08-27
Python从门到精通(五):文件处理-06-ini文件处理(python读ini文件)
这种类型的文件一般在脚本等一些工具程序中用处比较多,唯一一点注意的是ini的key不区分大小写。
文件内容
[installation]library=%(prefix)s/libinclude=%(prefix)s/includebin=%(prefix)s/binprefix=/usr/local# Setting related to debug configuration[debug]log_errors=trueshow_warnings=False[server]port: 8080nworkers: 32pid-file=/tmp/spam.pidroot=/ ================================= Brought to you by the Python Cookbook =================================
操作文件
from configparser import ConfigParsercfg = ConfigParser()cfg.read('test.ini')print(f'sections is: {cfg.sections()}')print(f"library is: {cfg.get('installation','library')}")print(f"log errors: {cfg.getboolean('debug','log_errors')}")print(f"port is: {cfg.getint('server','port')}")print(f"nworkers is: {cfg.getint('server','nworkers')}")print(f"signature is: {cfg.get('server','signature')}")#修改cfg.set('server','port','9000')cfg.set('debug','log_errors','False')print(f"new debug is: {cfg.getboolean('debug','log_errors')}")print(f"After change,port is: {cfg.getint('server','port')}")print(cfg.get('installation','PREFIX'))print(cfg.get('installation','prefix'))
sections is: ['installation', 'debug', 'server'] #配置文件分组,注意看.ini中的内容library is: /usr/local/liblog errors: Trueport is: 8080nworkers is: 32signature is: =================================Brought to you by the Python Cookbook=================================new debug is: FalseAfter change,port is: 9000/usr/local/usr/local
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~