Help needed on config files

Larry Bates larry.bates at websafe.com
Wed Jan 31 10:26:18 EST 2007


jvdb wrote:
> Hi there,
> 
> I am quite new on python programming and need some help on solving my
> problem..
> 
> I have to make a (python) program which deletes files from
> directories. I don't think deleting, etc. is the problem. The problem
> is that the directories where i have to delete them are 'dynamic'/
> subject to change. So what i thought is to make a config file
> containing an identifier (useful for myself) and there the directory.
> something like:
> [PROJECTx]
> <path>
> [PROJECTy]
> <path>
> 
> I have already seen that there are sorts of modules where you can read
> a config file, but only when you know the key.. Can someone help me
> out on this? The configfile can be altered in time (as there are more
> projects coming where i have to delete files on a scheduled basis).
> 
> This is a good learning project for me, but i really don't see how to
> solve this.
> 
Others have answered your specific question, I thought I'd add my
2 cents.  As the config file grows you will need to have other
sections that are not PROJECT# sections.  I use the pattern:

from ConfigParser import ConfigParser
cfg = ConfigParser()
cfg.read("proj.cfg")
projectSections=[section for section in cfg.sections()
                 if x.startswith('PROJECT')]

#
# Note: sections are case sensitive
#
for project in projectSections:
    #
    # Do your work
    #

-Larry



This seems to work quite well.  I hope information helps.

-Larry



More information about the Python-list mailing list