python ConfigParser对配置文件进行
# -*- coding: UTF8 -*-
import ConfigParser
class myConfParser:
def __init__(self, conf_path):
self.fpath = conf_path
self.cf = ConfigParser.ConfigParser()
self.cf.read(self.fpath)
def __del__(self):
with open(self.fpath, 'w') as fh:
self.cf.write(fh)
fh.close()
def add_section(self, s):
sections = self.cf.sections()
if s in sections:
return
else:
self.cf.add_section(s)
def remove_section(self, s):
return self.cf.remove_section(s)
def get(self, s, o):
return self.cf.get(s, o)
def set(self, s, o, v):
if self.cf.has_section(s):
self.cf.set(s, o, v)
def remove_option(self, s, o):
if self.cf.has_section(s):
return self.cf.remove_option(s, o)
return False
def items(self, s):
return self.cf.items(s)
def sections(self):
return self.cf.sections()
def options(self, s):
return self.cf.options(s)
def add_section_content(self,s,*arg):
self.cf.set(s,arg[0],arg[1])
self.cf.write(open("Config.txt","w"))
if __name__ == '__main__':
config_file = 'config.txt'
app = myConfParser(config_file)
print app.sections()
print app.items('CmdWindow')
print app.options('CmdWindow')
print app.get('Update', 'confirm')
print app.get('window', 'top')
app.set('system', 'path', 'D:/')
print app.remove_option('Update', 'Retry')
app.set('newUpdate', 'Confirm', 'yes')
app.add_section('rightt')
print app.remove_section('right')
app.add_section('window')
app.add_section_content('window','pyqt','4')
#config.txt
[main]
uuid = eJwzMTAxMODlMgFSpmDKCEqZQSgLMGViyMsFAI+xBn4=
build = 1240
setup = 40400
fileexists = 000000000
qhistorymax = 50
[Update]
interval = 15
confirm = yes
Retry=bob
confirm=pop
[Quicktime]
left = 0
top = 0
width = 424
height = 260
state = 0
[GraphQt]
v = 0
h = 65
[window]
left = 124
top = 69
width = 1093
height = 614
state = 0
ts = 0.5
bs = 0.5
cs = 0.75
lts = 0
rts = 0
thp = 546
pyqt = 4
[CmdWindow]
z = 0.629999995231628
x = 454
y = 377
[volumn]
left = 383
top = 154
width = 592
height = 411
state = 0
font = "宋体", 8, [], [clWindowText], 134
color = -2147483643
ontop = 0
wrap = 0
[right]
l=1
t=2