2
我有一個cfg文件。在CFG文件中有如下一行:如何使用python修改配置文件?
[Environment]
automation_type=GFX ;available options: GEM, HEXAII
我想通過修改此行:
[Environment]
automation_type=ABC ;available options: GEM, HEXAII
我已經寫了這下面的代碼:
get_path_for_od_cfg = r"C:\Users\marahama\Desktop\Abdur\abc_MainReleaseFolder\OD\od\odConfig.cfg"
config = ConfigParser.RawConfigParser()
config.read(get_path_for_OpenDebug_cfg)
for sec in config.sections():
for attr in config.options(sec):
if sec =='Environment' and attr == 'automation_type':
config.set('Environment','automation_type','ABC')
with open(get_path_for_OpenDebug_cfg, 'wb') as configfile:
config.write(configfile)
執行後代碼,我得到
[Environment]
automation_type = ABC
";available options: GEM, HEXAII" this line is missing.
該行是一條評論。我不認爲'RawConfigParser'對象存儲註釋。 – 2013-04-25 13:20:22
這是因爲「可用選項:GEM,HEXAII」是一個評論。我認爲你應該將註釋移動到另一行,或者使用簡單的文本處理技術,而不是ConfigParser。 – Elazar 2013-04-25 13:22:07