ConfigParser 在输出文件中写入重复部分
我正在使用ConfigParser模块来写一个配置文件,这个文件里包含了一些收集到的服务器信息。我写的第一个部分是本地系统信息,但不知为什么,它似乎把这个部分写了两遍。
这是我用来写配置的代码:
def system_info(config_file):
cnf_file = open(config_file, 'w+')
config.add_section('System Information')
wmi_conn_local = wmi.WMI()
for OpSys in wmi_conn_local.Win32_OperatingSystem():
OpSys = OpSys.Caption
x64 = tools.is_64_bit()
if x64 is True:
config.set('System Information', 'System Architecture', '64-bit')
else:
config.set('System Information', 'System Architecture', '32-bit')
HostName = socket.gethostname()
config.set('System Information', 'Hostname', HostName)
config.set('System Information', 'OS', OpSys)
config.write(cnf_file)
cnf_file.close()
def sql_info(config_file):
cnf_file = open(config_file, 'a+')
SQLServer_raw = raw_input("Please enter the SQL Server name: ")
server_correct = tools.query_yes_no("Is %s correct?" % SQLServer_raw)
if server_correct is False:
SQLServer_raw = raw_input("Sorry about that. Please enter the correct SQLCMD Path: ")
SQLIP = socket.gethostbyname(SQLServer_raw)
config.add_section('SQL Server')
config.set('SQL Server', 'Server Name', SQLServer_raw)
config.set('SQL Server', 'Server IP', SQLIP)
SQL_User = raw_input("Please enter the username to the connection to the SQL server: ")
user_correct = tools.query_yes_no("Is %s correct?" % SQL_User)
if user_correct is False:
SQL_User = raw_input("Sorry about that. Please enter the correct SQL User: ")
config.set('SQL Server', 'SQL User', SQL_User)
SQL_Pass = raw_input("Please enter the password for the username you specified above: ")
pass_correct = tools.query_yes_no("Is %s correct?" % SQL_Pass)
if pass_correct is False:
SQL_Pass = raw_input("Sorry about that. Please enter the correct SQL Password: ")
config.set('SQL Server', 'SQL Password', SQL_Pass)
config.write(cnf_file)
cnf_file.close()
if setup_exists is False:
os.system('cls')
print header
print "Setup file not found. Creating file."
logger.info("Setup file not found. Creating file.")
print "Gathering System Information..."
logger.info("Gathering System Information...")
setup_config.system_info(config_file)
print "Gathering SQL Server Information..."
logger.info("Gathering SQL Server Information...")
setup_config.sql_info(config_file)
这是我在配置文件中看到的输出:
[System Information]
system architecture = 64-bit
hostname = FLA-7MartinezD
os = Microsoft Windows 7 Enterprise
[System Information]
system architecture = 64-bit
hostname = FLA-7MartinezD
os = Microsoft Windows 7 Enterprise
[SQL Server]
server name = sql1
server ip = 198.105.244.102
sql user = sa
sql password = password
我唯一能想到的就是,我在写系统信息的时候使用了w+方法,而在写SQL部分(以及后面要写的其他部分)时用了a+方法。我这样做是想让系统信息先写入,然后把其他部分追加上去,但我可能想错了。
2 个回答
0
这个问题是因为在两个被调用的函数里都用了两次
config.write(cnf_file)
cnf_file.close()
而且config
对象是全局的,所以当你第二次用cnf_file = open(config_file, 'a+')
打开文件时,你是用的追加模式,这样系统信息就会被添加到文件里。
你需要把这些信息提取出来,放到主程序里,或者更简单的方法是第二次打开文件时也用写入模式:
cnf_file = open(config_file, 'w+')
0
重新考虑一下你的布局。
- 为什么你的
config
变量是全局的? - 你希望它是全局的吗?
你遇到的问题是,config 对象是全局的,而你把它写了两次到一个文件里!
- 假设你调用了 system_info(..)。
- 此时 config 对象里有一个“系统信息”的部分。
你把 config 写入了一个文件:
[System Information] system architecture = 64-bit hostname = FLA-7MartinezD os = Microsoft Windows 7 Enterprise
然后你又调用了 sql_info()。
- 此时 config 对象里有“SQL 服务器”和“系统信息”两个部分。
你把 config 追加到文件里:
[System Information] system architecture = 64-bit hostname = FLA-7MartinezD os = Microsoft Windows 7 Enterprise [System Information] system architecture = 64-bit hostname = FLA-7MartinezD os = Microsoft Windows 7 Enterprise [SQL Server] server name = sql1 server ip = 198.105.244.102 sql user = sa sql password = password
使用全局 config 对象的解决方案
def system_info():
if not config.has_section('System Information'): # prevent DuplicateSectionError
config.add_section('System Information')
wmi_conn_local = wmi.WMI()
for OpSys in wmi_conn_local.Win32_OperatingSystem():
OpSys = OpSys.Caption
x64 = tools.is_64_bit()
if x64 is True:
config.set('System Information', 'System Architecture', '64-bit')
else:
config.set('System Information', 'System Architecture', '32-bit')
HostName = socket.gethostname()
config.set('System Information', 'Hostname', HostName)
config.set('System Information', 'OS', OpSys)
def sql_info():
SQLServer_raw = raw_input("Please enter the SQL Server name: ")
server_correct = tools.query_yes_no("Is %s correct?" % SQLServer_raw)
if server_correct is False:
SQLServer_raw = raw_input("Sorry about that. Please enter the correct SQLCMD Path: ")
SQLIP = socket.gethostbyname(SQLServer_raw)
if not config.has_section('SQL Server'): # prevent DuplicateSectionError
config.add_section('SQL Server')
config.set('SQL Server', 'Server Name', SQLServer_raw)
config.set('SQL Server', 'Server IP', SQLIP)
SQL_User = raw_input("Please enter the username to the connection to the SQL server: ")
user_correct = tools.query_yes_no("Is %s correct?" % SQL_User)
if user_correct is False:
SQL_User = raw_input("Sorry about that. Please enter the correct SQL User: ")
config.set('SQL Server', 'SQL User', SQL_User)
SQL_Pass = raw_input("Please enter the password for the username you specified above: ")
pass_correct = tools.query_yes_no("Is %s correct?" % SQL_Pass)
if pass_correct is False:
SQL_Pass = raw_input("Sorry about that. Please enter the correct SQL Password: ")
config.set('SQL Server', 'SQL Password', SQL_Pass)
if setup_exists is False:
os.system('cls')
print header
print "Setup file not found. Creating file."
logger.info("Setup file not found. Creating file.")
print "Gathering System Information..."
logger.info("Gathering System Information...")
setup_config.system_info()
print "Gathering SQL Server Information..."
logger.info("Gathering SQL Server Information...")
setup_config.sql_info()
with open(config_file, 'w') as cnf_file:
config.write(cnf_file)
使用局部 config 对象的解决方案(推荐)
def system_info(config):
if not config.has_section('System Information'): # prevent DuplicateSectionError
config.add_section('System Information')
wmi_conn_local = wmi.WMI()
for OpSys in wmi_conn_local.Win32_OperatingSystem():
OpSys = OpSys.Caption
x64 = tools.is_64_bit()
if x64 is True:
config.set('System Information', 'System Architecture', '64-bit')
else:
config.set('System Information', 'System Architecture', '32-bit')
HostName = socket.gethostname()
config.set('System Information', 'Hostname', HostName)
config.set('System Information', 'OS', OpSys)
return config
def sql_info(config):
SQLServer_raw = raw_input("Please enter the SQL Server name: ")
server_correct = tools.query_yes_no("Is %s correct?" % SQLServer_raw)
if server_correct is False:
SQLServer_raw = raw_input("Sorry about that. Please enter the correct SQLCMD Path: ")
SQLIP = socket.gethostbyname(SQLServer_raw)
if not config.has_section('SQL Server'): # prevent DuplicateSectionError
config.add_section('SQL Server')
config.set('SQL Server', 'Server Name', SQLServer_raw)
config.set('SQL Server', 'Server IP', SQLIP)
SQL_User = raw_input("Please enter the username to the connection to the SQL server: ")
user_correct = tools.query_yes_no("Is %s correct?" % SQL_User)
if user_correct is False:
SQL_User = raw_input("Sorry about that. Please enter the correct SQL User: ")
config.set('SQL Server', 'SQL User', SQL_User)
SQL_Pass = raw_input("Please enter the password for the username you specified above: ")
pass_correct = tools.query_yes_no("Is %s correct?" % SQL_Pass)
if pass_correct is False:
SQL_Pass = raw_input("Sorry about that. Please enter the correct SQL Password: ")
config.set('SQL Server', 'SQL Password', SQL_Pass)
return config
if setup_exists is False:
os.system('cls')
print header
print "Setup file not found. Creating file."
logger.info("Setup file not found. Creating file.")
config = ConfigParser.RawConfigParser()
print "Gathering System Information..."
logger.info("Gathering System Information...")
config = setup_config.system_info(config)
print "Gathering SQL Server Information..."
logger.info("Gathering SQL Server Information...")
config = setup_config.sql_info(config)
with open(config_file, 'w') as cnf_file:
config.write(cnf_file)