Help needed on config files

J

jvdb

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.
 
R

Rob Wolfe

jvdb said:
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>

What about this?

# proj.cfg
[PROJECTx]
path=/path/to/dir1
[PROJECTy]
path=/path/to/dir2

# cfg.py
from ConfigParser import ConfigParser
cfg = ConfigParser()
cfg.read("proj.cfg")
for proj in cfg.sections():
path = cfg.items(proj)[0][1]
print "proj: %s, path: %s" % (proj, path)

Is that not enough?
 
D

Daniele Varrazzo

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

The classes defined in the ConfigParser module contain a 'sections()'
method that do what you need.

http://docs.python.org/lib/module-ConfigParser.html

Daniele
 
L

Larry Bates

jvdb said:
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
 
J

jvdb

Ok, with all your help and very useful hints, i managed to solve it!
thanks!
Now my program loops through the config file, and deletes the files
older than 7 days with a specified extension.
Here's my program:
#this project removes old log files

import os, time
from ConfigParser import ConfigParser
now=time.time()

cfg = ConfigParser()
cfg.read("projects.cfg")

#loop through the config file
for proj in cfg.sections():
print ""
#get the path where file files should be
path = cfg.items(proj)[1][1]
#get the filetype
ftype = cfg.items(proj)[0][1]
print "project: %s, path to old logfiles: %s" % (proj, path)
dirList=os.listdir(path)
# Now check if the file is not a dir and then check if the file is
older than 7 days
for fname in dirList:
if fname.endswith(ftype):
pad = path+"\\"+fname
if os.stat(pad).st_mtime < now - 7 * 86400:
if os.path.isfile(pad):
os.remove(pad)
print "The file %s is deleted" % (fname)



===========
#projects.cfg
[ebiz_factuur]
dir=c:\customer\ebiz_factuur
type=.log
[test]
dir=c:\temp
type=.txt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,200
Latest member
SCPKatheri

Latest Threads

Top