【问题标题】:ConfigParser - remove_section don't remove sectionConfigParser - remove_section 不删除部分
【发布时间】:2015-07-09 14:11:12
【问题描述】:

我有工具,用于生成和显示配置(部分)。

INI 文件看起来像:

...

[test]
MATH_DLL = 035f210e-6c06-4021-8857-0759409c4bb7
MATH_DLL_XML = 805fe0f0-2627-4ced-bac5-8725ef9839ef
ASSETMANAGER_API_DLL = 426d1824-628f-47ed-8539-4a0ed292b94e
...

我现在想要的 - 添加选项以将部分删除到“主”文件:

...
parser_unity.add_argument('-x', '--remove', action='store', dest='unity_uuid_remove',
                          help='Remove configuration from uuids.ini. Must be called with config_name to delete')
...

还有:

def handler_unity(action_list):
...
    if action_list.unity_uuid_remove:
        from lib.unity.uuidgen import UuuidManagment
        u = UuuidManagment(logger, RDS_BASEDIR)
        u.config_remove(action_list.unity_uuid_remove)

还有类和方法:

class UuuidManagment(object):

    """Class for data in uuids.ini file management"""

    def __init__(self, logger, rds_basedir):

        self.config = ConfigParser.ConfigParser()
        self.config_file = os.path.join(rds_basedir, 'conf', 'unity', 'uuids.ini')
        self.config.read(self.config_file)
        self.configs = self.config.sections()

        self.logger = logger
        self.rds_basedir = rds_basedir

    ...

    def config_remove(self, config_name):

        """Remove config_name specified in argument from uuids.ini"""

        self.logger.logger.info('Remove %s' % config_name)

        print(self.config, self.configs, config_name)

        self.config.remove_section(config_name)

        with open(self.config_file, 'r+') as out:
            self.config.write(out)

但它不想工作。

当前配置:

d:\RDS\rdsmanager>RDSmanager.py unity -l                                                                                              
RDSmanager started at 09, Jul 2015 at 17:05:35                                                                                        
Running configlist                                                                                                                    

Currently avalable configurations:                                                                                                    

develop                                                                                                                               
debug                                                                                                                                 
qa                                                                                                                                    
version_1_0_2_staging                                                                                                                 
test                                    

删除它:

d:\RDS\rdsmanager>RDSmanager.py unity -x test                                                                                         
RDSmanager started at 09, Jul 2015 at 17:05:40                                                                                        
Remove test                                                                                                                           
(<lib.external.ConfigParser.ConfigParser instance at 0x02346580>, ['develop', 'debug', 'qa', 'version_1_0_2_staging', 'test'], 'test')

再次检查:

d:\RDS\rdsmanager>RDSmanager.py unity -l                                                                                              
RDSmanager started at 09, Jul 2015 at 17:05:42                                                                                        
Running configlist                                                                                                                    

Currently avalable configurations:                                                                                                    

develop                                                                                                                               
debug                                                                                                                                 
qa                                                                                                                                    
version_1_0_2_staging                                                                                                                 
test                                         

【问题讨论】:

  • @Gall 抱歉,不明白...请您澄清一下吗?
  • 如果你使用w而不是r+来更新文件呢?
  • @Gall 请将其添加为答案 :-)(仅供参考 - 'w+' 有效)

标签: python python-2.7 ini


【解决方案1】:

config_remove 中,您以r+ 模式打开文件。

您应该以写入/截断模式打开它w 以正确重写文件,如您在doc 中所见。

'w' 用于写入(如果文件已存在则截断)

更多信息,这个问题:Confused by python file mode “w+”+变体的实际用途有关。

【讨论】:

  • 谢谢。我改为:with open(self.config_file, 'w+') as out - 现在可以了。愚蠢的问题:-)
  • @setevoy 我添加了指向与+ 变体相关的问题的链接,以获取有关更适合使用什么的更多信息。
猜你喜欢
  • 1970-01-01
  • 2016-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多