Editing File

Jeremy Jones jemejones at gmail.com
Wed Jul 12 09:54:01 EDT 2006


D wrote:
> Hi, I currently have a Python app with a Tkinter GUI frontend that I
> use for system administration.  Everytime it launches, it reads a text
> file which contains info about each host I wish to monitor - each field
> (such as IP, hostname, etc.) is delimited by !!.  Now, I want to be
> able to edit host information from within the GUI - what would  be the
> best way to go about this?  Basically I just need to either edit the
> original host line, or write a new host line and delete the
> original..thanks!

I would create a data structure of the contents of the file and let the
application reference that data structure.  Sounds like it's going to
be a list of lists or a list of dicts.  Each line of the file is going
to be an element of the "main" list.  Each element of the list is going
to be a dict or a list of the details of that particular host.  Make it
so that if your app changes the datastructure, you re-serialize it back
to the file.  This should work the same with adding a new host to
monitor.

It might be easier to use something like Yaml.  I'm doing something
similar with a little podcast grabber I'm working on.  Here's some old
code where I first incorporate using Yaml (down at the bottom of the
page): http://jeremymjones.com/articles/simple-podcast-grabber-python/
The version I have in SVN right now creates a configgish object off of
the Yaml and on re-assignment of either of the two main attributes, it
automatically reserializes it.

Anyway, hope this helps.

- Jeremy M. Jones




More information about the Python-list mailing list