使用ConfigParser(Python)编辑ini文件选项值

时间:2010-08-10 04:30:27

标签: python ini configparser

任何人都知道如何使用ConfigParser编辑ini文件值? (或者甚至一个开始的地方都会很棒!)我的配置文件中有很多注释,所以我想通过编辑值来保留它们,而不是取值并使用多个文件。< / p>

我的配置文件的结构:

[name1]
URL = http://example.com
username = dog
password = password

[name2]
URL = http://catlover.com
username = cat
password = adffa

正如您所看到的,我为不同的部分名称提供了相同的选项,因此如果ConfigParser无法完成,只编辑一个部分的值会有点棘手。

提前致谢。

1 个答案:

答案 0 :(得分:2)

这是一个例子

import sys
import os.path
from ConfigParser import RawConfigParser as ConfParser
from ConfigParser import Error

p = ConfParser()
# this happend to me save as ASCII
o = open("config.ini")
if o.read().startswith("\xef\xbb\xbf"):
    print "Fatal Error; Please save the file as ASCII not unicode."
    sys.exit()
try:
    results = p.read("config.ini")
except Error, msg:
    print "Error Parsing File"
    print msg
else:
    if results == []:
        print "Could not load config.ini."
        if not os.path.exists("config.ini"):
            print "config.ini does not exist."

        else:
            print "An uknown error occurred."

    else:
        print "Config Details"
        sections = p.sections()
        sections.sort()
        for s in sections:
            print "------------------------"
            print s
            if p.has_option(s, "URL"):
                print "URL: ",
                print p.get(s, "URL")

            else:
                print "URL: No Entry"

            if p.has_option(s, "username"):
                print "User: ",
                print p.get(s, "username")

            else:
                print "User: N/A"

            if p.has_option(s, "password"):
                print "Password: ",
                print p.get(s, "password")

            else:
                print "Password: N/A"

此外,我创建了这个类来存储我的应用程序变量等,也使配置编写更容易,它最初与扭曲使用但我创建了一个简单的替换记录器

import os.path
import sys
#from twisted.python import log
import ConfigParser
from traceback import print_last
class Log(object):

    def msg(t):
        print "Logger: %s " % t

    def err(t = None):
        print "-------------Error-----------"
        print "\n\n"
        if t is None:
            print_last()
# sloppy replacement for twisted's logging functions
log = Log()            
class Settings(object):
    '''Stores settings'''
    config_variables = ['variables_that_should_be_stored_in_config']
    def __init__(self, main_folder = None, log_file = None, music_folder = None ):
        # load the defaults then see if there are updates ones in the config 
        self.load_defaults()
        self.config = ConfigParser.RawConfigParser()
        if len(self.config.read(self.settings_file)) == 1:
            if 'Settings' in self.config.sections():
                try:
                    self.music_folder = self.config.get('Settings', 'music_folder')
                except ConfigParser.NoOptionError:
                    pass
                log.msg('Music Folder: %s' % self.music_folder)
                try:
                    self.mplayer = self.config.get('Settings', 'mplayer')
                except ConfigParser.NoOptionError:
                    pass
                try:
                    self.eula = self.config.getboolean('Settings', 'eula')
                except ConfigParser.NoOptionError:
                    pass
            else:
                log.msg('No Settings Section; Defaults Loaded')
        else:
            log.msg('Settings at default')
    def load_defaults(self):
        log.msg('Loading Defaults')
        self.main_folder = os.path.dirname(os.path.abspath(sys.argv[0]))
        self.settings_file = os.path.join(self.main_folder, 'settings.cfg')
        self.log_file = os.path.join(self.main_folder, 'grooveshark.log')
        self.music_folder = os.path.join(self.main_folder, 'Music')
        self.grooveshark_started = False
        self.eula = False
        self.download_percent = 0.5# default buffer percent is 50 % 
        if sys.platform == 'win32' or sys.platform == 'cygwin':# Windows
            if os.path.exists( os.path.join(self.main_folder, 'mplayer', 'mplayer.exe') ):
                self.mplayer = os.path.join(self.main_folder, 'mplayer', 'mplayer.exe')
            elif os.path.exists( os.path.join(self.main_folder, '/mplayer.exe') ):
                self.mplayer = os.path.join(self.main_folder, '/mplayer.exe') 
            else:
                self.mplayer = 'download'

        elif sys.platform == 'darwin':# Mac
            if os.path.exists( os.path.join(self.main_folder, 'mplayer/mplayer.app') ):
                self.mplayer = os.path.join(self.main_folder, 'mplayer/mplayer.app')
            elif os.path.exists( os.path.join(self.main_folder, '/mplayer.app') ):
                self.mplayer = os.path.join(self.main_folder, '/mplayer.app') 
            else:
                self.mplayer = 'download'
        else:# linux
            # download or navigate to it
            self.mplayer = 'download'

        # Create Music Folder if it does not exist
        if not os.path.exists(self.music_folder):
            os.makedirs(self.music_folder)
        # Create log file if it does not exist
        if not os.path.exists(self.log_file):
            l = open(self.log_file, 'wb')
            l.close()

        log.msg('Application Folder: %s' % self.main_folder)
        log.msg('Log File: %s' % self.log_file)
        log.msg('Music Folder: %s' % self.music_folder)

    def __setattr__(self, variable, value):
        log.msg('Setting %s to %s' % (variable, value))
        object.__setattr__(self, variable, value)
        if variable in self.config_variables:
            try:
                self.config.set('Settings', variable, value)
            except:
                # Means config wasn't created then, could be we were trying to set self.config (in which case self.config wasn't set yet because we were trying to set it)
                log.err()
            else:
                # UPDATE settings file
                log.msg('Saving Settings to %s' % (self.settings_file))
                try:
                    self.config.write( open(self.settings_file, 'wb') )
                except:
                    log.err()