使用Configparser或propparser写入更改属性的情况

时间:2015-11-04 10:34:38

标签: python configparser

我使用configparser或propparser从文件中读取属性,如果不存在则添加它

但是使用configparser或propparser添加属性正在改变剩余属性的情况

以下是我的剧本

from ConfigParser import  RawConfigParser

def modifyInstallProperties ():
   propParser = RawConfigParser()
   propParser.read('/home/ram/test.properties')

   try:
     ip = propParser.get('Client', 'export_sftp_identity_file')
     print "ip is " +ip
   except :
     propParser.set('Client','#export_sftp_identity_file','/home/ram/.ssh/id_rsa')
     with open('/home/ram/test.properties', 'w') as configfile:
        propParser.write(configfile)

modifyInstallProperties()

我正在写属性“export_sftp_identity_file”,如果它不存在,但它改变了剩余属性的情况

before                                                 after
base_server_IP = 172.31.1.52                         base_server_ip = 172.31.1.52 (ip instead of IP)
installShard = false                                 installshard = false ( s instead of S in shard)
installDbServer = false                              installdbserver = false (d instead of D in Dbserver)
....

test.properties,在运行脚本之前

[Client]
enable_https_from_patch = false
client_install_dir = /centina/client
export_sftp_target_port = 22
client_jboss_dir = /centina/client/jboss-eap-5.2.0
base_server_IP = 172.31.1.52
export_sftp_target_host = 172.31.1.52
enable_https = false
export_sftp_target_password = centina
enable_http = true
export_sftp_target_user = centina

[Common]
db_pm_port = 3306
installShard = false
db_sla_port = 3306
jdk_dir = /opt/jdk
installDbServer = false
installDbMultiple = false
installOnlyDbServer = false
installClientServer = false
installLoader = false
installMedMultiple = false
db_sa_port = 3306
db_server_IP = 172.31.1.52
installDbSlave = false
installOnlyMedServer = false
server_bind_ip = 0.0.0.0
installMedServer = false
db_fm_port = 3306
standAloneInstallation = false
installAppServer = false
db_spm_port = 3306

运行脚本后

[Client]
enable_https_from_patch = false
client_install_dir = /centina/client
export_sftp_target_port = 22
client_jboss_dir = /centina/client/jboss-eap-5.2.0
base_server_ip = 172.31.1.52
export_sftp_target_host = 172.31.1.52
enable_https = false
export_sftp_target_password = centina
enable_http = true
export_sftp_target_user = centina
#export_sftp_identity_file = /home/centina/.ssh/id_rsa

[Common]
db_pm_port = 3306
installshard = false
db_sla_port = 3306
jdk_dir = /opt/jdk
installdbserver = false
installdbmultiple = false
installonlydbserver = false
installclientserver = false
installloader = false
installmedmultiple = false
db_sa_port = 3306
db_server_ip = 172.31.1.52
installdbslave = false
installonlymedserver = false
server_bind_ip = 0.0.0.0
installmedserver = false
db_fm_port = 3306
standaloneinstallation = false
installappserver = false
db_spm_port = 3306

How to solve this

0 个答案:

没有答案