ConfigParser problem Python -
i'm having problem append config file. here's want create;
[section1] val1 = val2 val3 = val4
but when run following code see configparser.nosectionerror: no section: 'section1'
import configparser cfg = configparser.rawconfigparser() cfg.set("section1", "val1", "val2") f = open("example.cfg", "a") cfg.write(f)
if add
if not cfg.has_section("section1"): cfg.add_section("section1")
and then, get;
[section1] val1 = val2 [section1] val3 = val4
could point me i'm doing wrong? thanks
i fleshed out code put bit. reading existing file before checking section? also, should writing whole file @ once. don't append.
import configparser cfg = configparser.configparser() cfg.read('example.cfg') if not cfg.has_section('section1'): cfg.add_section('section1') cfg.set('section1', 'val1', 'val2') cfg.set('section1', 'val2', 'val3') f = open('example.cfg', 'w') cfg.write(f) f.close()
Comments
Post a Comment