mailbox.Maildir question/problem

tinnews at isbd.co.uk tinnews at isbd.co.uk
Fri Dec 14 05:01:28 EST 2007


Ross Ridge <rridge at caffeine.csclub.uwaterloo.ca> wrote:
> <tinnews at isbd.co.uk> wrote:
> >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



More information about the Python-list mailing list