473,396 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help needed on config files

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.

Jan 31 '07 #1
5 1465

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>
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?

--
HTH,
Rob

Jan 31 '07 #2
On Jan 31, 11:04 am, "jvdb" <streamserv...@gmail.comwrote:
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

Jan 31 '07 #3
Yes! That does the trick, thanks, both of you!

Jan 31 '07 #4
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
Jan 31 '07 #5
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

Jan 31 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Benne Smith | last post by:
In our company, i have three servers; 1) a development server (mine only - here i make daily changes and test my stuff) 2) a test server (for the users to test milestone builds - changes weekly)...
0
by: Jon | last post by:
We have a asp.net app that runs like the following in a single-site scenario. Directory structure: c:\inetpub\wwwroot\myapp - aspx files, global.asax and web.config c:\inetpub\wwwroot\myapp\bin...
13
by: Siegfried Heintze | last post by:
I refered the engineer at my hosting service to http://support.microsoft.com/default.aspx?scid=kb;en-us;825738 where he tried to follow the directions there. He said there was no such file:...
1
by: Alexandre CONRAD | last post by:
Hello list ! I'm using the ConfigParser module to use configuration files (what else would it be for ?). But I have a dilema: I'd like to setup multiple "update server" for my application with...
2
by: Preetam Pattanashetty | last post by:
Hi I am learning ASP.NET using C#. I am able to run .aspx files on local system, but when I load them to the server, I get the "Server Error in '/' Application" error. I have tried to configure the...
1
by: apondu | last post by:
Hello friends.. I needed some help on ASP.Net web application along with its web service deployment I have a situation where i am not able to understand and confused a bit on how to proceed...
5
by: ibid | last post by:
hi every one just wondering if anyone could help sorry if i seem abit dumb but im a newbie to it all ive got to asign a mail program on my auction site in the config.pl files (i think this is the...
0
by: Luis Speciale | last post by:
Hi u all : I'm trying to build mod_python in Leopard 10.5.4 with a cvs version from http://svn.apache.org/repos/asf/quetzalcoatl/mod_python/trunk I have $ python Python 2.5.2...
2
by: Luis Speciale | last post by:
Hi I'm trying to build mod_pyton on Leopard 10.5.4 on a Mac G5 with this cvs version http://svn.apache.org/repos/asf/quetzalcoatl/mod_python/trunk with this Python python Python 2.5.2...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.