473,408 Members | 1,730 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,408 software developers and data experts.

mailbox.Maildir question/problem

I am trying to write a utility to remove empty maildir mailboxes. It
sounds like this should be very simple but it's proving really
difficult.

I'm doing this on a Fedora 7 system with python 2.5.

The first question is how to detect whether a directory is a maildir
mailbox. The following code snippet *never* says that a directory is
not a maildir:-

try:
x = mailbox.Maildir(dirpath, None, False)
except:
print dirpath, "is not a maildir"

The "x = mailbox.Maildir(dirpath, None, False)" always succeeds even
when dirpath is most definitely *not* a maildir (i.e. it doesn't have
cur, new and tmp sub-directories). It's only when you try calling a
method of x that an exception results.
The second question is how to manage a hierarchy of directories with
maildirs in them. For example I have:-

Mail
Mail/bcs
Mail/ben
Mail/cg
Mail/spam
Mail/usenet

Where bcs ben cg spam usenet are maildir mailboxes, I can't get
python's mailbox.Maildir to do anything useful with them at all.

My test program currently is:-

#!/usr/bin/python
#
#
# Remove empty maildir mailboxes
#
import mailbox
import os.path
import sys

def checkDir(dummy, dirpath, filelist):
print "Directory is ", dirpath
try:
x = mailbox.Maildir(dirpath, None, False).list_folders()
except:
print dirpath, "is not a maildir"
return
for msg in x:
print msg

for d in sys.argv[1:]:
if os.path.isdir(d):
os.path.walk(d, checkDir, None)

It would seem that the list_folders() method only works with the
Courier style maildirs where the diretory name of the maildir starts
with a dot. Is there *any* way I can get python to access maildirs
which are not named using this (IMHO stupid) convention?

I know my test program is far from complete but I can't get it to do
anything sensible at present.

--
Chris Green
Dec 11 '07 #1
2 1988
<ti*****@isbd.co.ukwrote:
>Is there *any* way I can get python to access maildirs
which are not named using this (IMHO stupid) convention?
Well, the mailbox module doesn't support deleting mailboxes, so I'm not
sure why you want to use it. Since you also seem to have a better idea
of what your maildirs look like, why not just use the lower level file
functions directly? Something like:

def remove_empty_maildir(dirname):
expected = set(["cur", "new", "tmp"])
ents = set(os.listdir(dirname))
if ents != expected:
if expected.issubset(ents):
raise error, "unexpected subdirs in maildir"
raise error, "not a maildir"
subdirs = [os.path.join(dirname, d)
for d in expected]
for d in subdirs:
if len(os.listdir(d)) != 0:
return False
for d in subdirs:
os.rmdir(d)
os.rmdir(dirname)
return True

Your case is presumably different somehow, so you'll have to update and
fix this completely untested code if you want to use it.

Ross Ridge

--
l/ // Ross Ridge -- The Great HTMU
[oo][oo] rr****@csclub.uwaterloo.ca
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
Dec 13 '07 #2
Ross Ridge <rr****@caffeine.csclub.uwaterloo.cawrote:
<ti*****@isbd.co.ukwrote:
Is there *any* way I can get python to access maildirs
which are not named using this (IMHO stupid) convention?

Well, the mailbox module doesn't support deleting mailboxes, so I'm not
sure why you want to use it.
I was hoping to be able to use it for other things as well as deleting
mailboxes.

Since you also seem to have a better idea
of what your maildirs look like, why not just use the lower level file
functions directly? Something like:

def remove_empty_maildir(dirname):
expected = set(["cur", "new", "tmp"])
ents = set(os.listdir(dirname))
if ents != expected:
if expected.issubset(ents):
raise error, "unexpected subdirs in maildir"
raise error, "not a maildir"
subdirs = [os.path.join(dirname, d)
for d in expected]
for d in subdirs:
if len(os.listdir(d)) != 0:
return False
for d in subdirs:
os.rmdir(d)
os.rmdir(dirname)
return True

Your case is presumably different somehow, so you'll have to update and
fix this completely untested code if you want to use it.
I guess I will have to do something like this but the problem is more
subtle than that, what if another program writes a new message to the
mailbox just after you've checked that cur, new and tmp are all empty?
The whole point of maildir is that locking isn't needed and I was
hoping that the maildir() object in python would encapsulate correct
handling of the maildir including deletion.

As it is I will have to write code to do the correct handling,
presumably one checks the new directory last before deleting the whole
maildir and, if the deletion fails, someone must have put something
there.

--
Chris Green
Dec 14 '07 #3

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

Similar topics

1
by: Julia Goolia | last post by:
hello everyone, i have the following problem. I am trying to create maildir directories on the local filesystem then chmod, chown them to courier:courier (for courier mail server). the courier...
1
by: Matej Cepl | last post by:
Hi, is there any example of use of mailbox.py. I would be especially interested if anybody made a objects which would be able to .delete() ..append() etc. individual messages. Thanks, Matej
4
by: Chuck Amadi | last post by:
Has anyone got a simple python script that will parse a linux mbox and create a large file to view . Cheers Chu
6
by: chuck amadi | last post by:
Hi , Im trying to parse a specific users mailbox (testwwws) and output the body of the messages to a file ,that file will then be loaded into a PostGresql DB at some point . I have read the...
0
by: David Stockwell | last post by:
Hi, After looking at http://docs.python.org/lib/module-mailbox.html I am a bit confused. The bottom of the page refers to some shortcut usage for getting a mail box import email import...
3
by: noah | last post by:
Is there a way to figure out what filename an email object points to in a qmail style Maildir directory? Hmmm... I don't think so, but I'm hoping I wrong. I instantiated a Maildir mailbox and...
5
by: Michael | last post by:
Hello, I've created an ASP web page where users in our organization can create Active Directory computer accounts. The web page is running on a Server 2003 SP1 IIS 6 installation. The...
0
by: arjen1984 | last post by:
I want to create a mailbox-enabled mailbox. First i have used the toturial from microsoft: http://support.microsoft.com/kb/313114 Then my homeMDB string was incorrect. I saw an article , link:...
0
by: jesse.k.rosenthal | last post by:
Dear all, I'm trying to figure out if mailbox will let me get the flags of a Maildir message without running get_message on it, and reading the whole message. It seems like it should work by...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.