[英]Modify INI file with Python
I have an INI file I need to modify using Python. 我有一个我需要使用Python修改的INI文件。 I was looking into the
ConfigParser
module but am still having trouble. 我正在调查
ConfigParser
模块但仍然遇到问题。 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. 但是在运行此代码后查看
amcap.ini
文件时,它仍未修改。 Can anyone tell me what I am doing wrong? 谁能告诉我我做错了什么?
ConfigParser does not automatically write back to the file on disk. ConfigParser不会自动写回磁盘上的文件。 Use the
.write()
method for that; 使用
.write()
方法 ; 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.