Marching on: populating lists or tuples from textfiles - manipulating textfiles

Terry Reedy tjreedy at udel.edu
Wed Aug 28 02:22:47 CEST 2002


"Micah Mayo" <astrophels at yahoo.com> wrote in message
news:57e0b28.0208271327.627d8b0a at posting.google.com...
...
> What I'd like to do is this: Have three 'config' files for the
program
> to read. The first would tell the program which files to copy, for
> example. Here is the syntax for this one:
> # ServiceName    SourcePath         DestinationPath
> BIND             /mnt/named.conf -> /etc/namedb/named.conf
> BIND             /mnt/db.foo.com -> /etc/namedb/db.foo.com
> BIND             /mnt/db.bar.net -> /etc/namedb/db.bar.net
> HTTPD            /mnt/httpd.conf -> /usr/apache2/httpd.conf
> etc..
> etc..
>
> I'd like python to read that file, populate a list, or possibly a
> tuple with the correct information, and then run my file moving
method

Call that file movelist and leave out '->' but include space or tab
between.
Then here's a start:

def bind(s,d): #whatever
def httpd(s,d): # ditto

services = { #dispatch dict
  'BIND': bind, # service name to service function binding
 'HTTPD': httpd,
 ...
}

f = file('movelist')
for l in f.readlines:
  ser,src,dst = l.split()
  services[ser](src,dst) # call appropriate funct with two params

Terry J. Reedy






More information about the Python-list mailing list