我有工具,用于生成和显示配置(部分)。
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 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
答案 0 :(得分:1)
在config_remove
中,您以r+
模式打开文件。
您应该以write / truncate模式w
打开它以正确地重写文件,如doc中所示。
'w'用于写入(如果文件已存在则截断文件)
有关详情,请提出以下问题:Confused by python file mode “w+”与+
变种的实际目的相关。