【发布时间】:2009-09-14 18:35:35
【问题描述】:
我有这个 python 代码用于打开一个 .cfg 文件,写入并保存它:
import ConfigParser
def get_lock_file():
cf = ConfigParser.ConfigParser()
cf.read("svn.lock")
return cf
def save_lock_file(configurationParser):
cf = configurationParser
config_file = open('svn.lock', 'w')
cf.write(config_file)
config_file.close()
这看起来很正常还是我错过了有关如何打开-写入-保存文件的内容?有没有更标准的方式来读写配置文件?
我问是因为我有两种方法似乎做同样的事情,它们获取配置文件句柄 ('cf') 调用 cf.set('blah', 'foo' bar) 然后使用 save_lock_file(cf)调用上面。对于一种方法它有效,而对于另一种方法,写入永远不会发生,现在不确定为什么。
def used_like_this():
cf = get_lock_file()
cf.set('some_prop_section', 'some_prop', 'some_value')
save_lock_file(cf)
【问题讨论】:
标签: python file file-io configuration-files