Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify INI file with Python

I have an INI file I need to modify using Python. I was looking into the ConfigParser module but am still having trouble. My code goes like this:

config= ConfigParser.RawConfigParser()
config.read('C:\itb\itb\Webcams\AMCap1\amcap.ini')
config.set('Video','Path','C:\itb\itb')

But when looking at the amcap.ini file after running this code, it remains unmodified. Can anyone tell me what I am doing wrong?

like image 351
Kreuzade Avatar asked Jul 24 '12 18:07

Kreuzade


1 Answers

ConfigParser does not automatically write back to the file on disk. Use the .write() method for that; it takes an open file object as it's argument.

config= ConfigParser.RawConfigParser()
config.read(r'C:\itb\itb\Webcams\AMCap1\amcap.ini')
config.set('Video','Path',r'C:\itb\itb')
with open(r'C:\itb\itb\Webcams\AMCap1\amcap.ini', 'wb') as configfile:
    config.write(configfile)
like image 54
Martijn Pieters Avatar answered Sep 23 '22 19:09

Martijn Pieters