mailbox.Maildir question/problem

tinnews at isbd.co.uk tinnews at isbd.co.uk
Tue Dec 11 16:15:04 EST 2007


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



More information about the Python-list mailing list