Best way to update a settings file?

John Nagle nagle at animats.com
Mon Apr 14 05:48:43 CEST 2008


John Salerno wrote:
> I'm thinking about writing a small script that will update an xml file 
> with whatever game settings the user enters. I imagine that the user 
> will have a single-screen GUI application with several different 
> settings, like this:
> 
> CROSSHAIRS   ON
> LOCATION     ON
> HEALTHBAR    OFF
> etc.....
> 
> These settings will somehow be toggleable with an ON/OFF button, or 
> something.
> 
> Anyway, I can think of two ways to do this:
> 
> 1. After the user chooses his settings and clicks the Save button, 
> Python reads all the settings from the GUI application and updates 
> *every* entry in the xml file (whether it has changed or not).
> 
> 2. After the user chooses his settings and clicks the Save button, 
> Python reads all the settings from the GUI application, compares these 
> settings to those in the xml file, and updates only the ones that have 
> been changed.

    You can't write into the middle of an XML file effectively; any field
that has changed length won't fit.  You generally have to create a new
XML file.

    Read this on how to replace a file with a new one, as an atomic operation:

	http://blogs.msdn.com/adioltean/archive/2005/12/28/507866.aspx

    If you really want to update files in place, use a database, like
SQLite.  If you find yourself rewriting big files for minor changes,
switch to a database.  For small files, just rewrite the whole thing.

					John Nagle



More information about the Python-list mailing list