How to save a configuration file / python file IO

╄→гoц情女王★ 提交于 2019-12-08 11:42:19

问题


I have this python code for opening a .cfg file, writing to it and saving it:

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()

Does this seem normal or am I missing something about how to open-write-save files? Is there a more standard way to read and write config files?

I ask because I have two methods that seem to do the same thing, they get the config file handle ('cf') call cf.set('blah', 'foo' bar) then use the save_lock_file(cf) call above. For one method it works and for the other method the write never takes place, unsure why at this point.

def used_like_this():
        cf = get_lock_file()
        cf.set('some_prop_section', 'some_prop', 'some_value')
        save_lock_file(cf)

回答1:


Just to note that configuration file handling is simpler with ConfigObj.

To read and then write a config file:

from configobj import ConfigObj
config = ConfigObj(filename)

value = config['entry']
config['entry'] = newvalue
config.write()



回答2:


Looks good to me.

If both places call get_lock_file, then cf.set(...), and then save_lock_file, and no exceptions are raised, this should work.

If you have different threads or processes accessing the same file you could have a race condition:

  1. thread/process A reads the file
  2. thread/process B reads the file
  3. thread/process A updates the file
  4. thread/process B updates the file

Now the file only contains B's updates, not A's.

Also, for safe file writing, don't forget the with statement (Python 2.5 and up), it'll save you a try/finally (which you should be using if you're not using with). From ConfigParser's docs:

with open('example.cfg', 'wb') as configfile:
  config.write(configfile)



回答3:


Works for me.

C:\temp>type svn.lock
[some_prop_section]
Hello=World

C:\temp>python
ActivePython 2.6.2.2 (ActiveState Software Inc.) based on
Python 2.6.2 (r262:71600, Apr 21 2009, 15:05:37) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 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()
...
>>> def used_like_this():
...         cf = get_lock_file()
...         cf.set('some_prop_section', 'some_prop', 'some_value')
...         save_lock_file(cf)
...
>>> used_like_this()
>>> ^Z


C:\temp>type svn.lock
[some_prop_section]
hello = World
some_prop = some_value


C:\temp>


来源:https://stackoverflow.com/questions/1423214/how-to-save-a-configuration-file-python-file-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!