ConfigParser - remove_section 不要删除部分
ConfigParser - remove_section don't remove section
我有工具,用于生成和显示配置(部分)。
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
...
我现在想要的 - 添加选项以删除 "main" 文件的部分:
...
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和方法:
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
在 config_remove
中,您以 r+
模式打开文件。
您应该在 write/truncate 模式下打开它 w
以正确重写文件,如您在 doc.
中所见
'w' for writing (truncating the file if it already exists)
有关更多信息,这个问题:Confused by python file mode “w+” 与 +
变体的实际目的有关。
我有工具,用于生成和显示配置(部分)。
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
...
我现在想要的 - 添加选项以删除 "main" 文件的部分:
...
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和方法:
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
在 config_remove
中,您以 r+
模式打开文件。
您应该在 write/truncate 模式下打开它 w
以正确重写文件,如您在 doc.
'w' for writing (truncating the file if it already exists)
有关更多信息,这个问题:Confused by python file mode “w+” 与 +
变体的实际目的有关。