Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to test for maildir 'folder' in Python?

45 views
Skip to first unread message

Chris Green

unread,
Jan 22, 2022, 4:33:19 PM1/22/22
to
I have a script that walks a quite deep tree of mail messages to find
and archive old messages. I'm trying to convert it from mbox to
maildir (as I now store my mail in maildir format).

So I need to test whether a point I have reached in the hierarchy is a
maildir mailbox or not. Using mbox format it's easy because 'folders'
are directories and mailboxes are files. However with maildir the
'folders' have directories within them so the simple tree walking goes
down a level too far and finds 'folders' which aren't mailboxes called
'cur', 'new' and 'tmp'.

Is there any 'ready made' way in python to tell whether a directory is
a maildir mailbox? If not I suppose I'll simply have to check if
there are 'cur', 'new' and 'tmp' directories within the directory
which may or may not be a maildir.

--
Chris Green
·

Cameron Simpson

unread,
Jan 22, 2022, 6:44:04 PM1/22/22
to
On 22Jan2022 21:26, Chris Green <c...@isbd.net> wrote:
>So I need to test whether a point I have reached in the hierarchy is a
>maildir mailbox or not. Using mbox format it's easy because 'folders'
>are directories and mailboxes are files. However with maildir the
>'folders' have directories within them so the simple tree walking goes
>down a level too far and finds 'folders' which aren't mailboxes called
>'cur', 'new' and 'tmp'.
>
>Is there any 'ready made' way in python to tell whether a directory is
>a maildir mailbox? If not I suppose I'll simply have to check if
>there are 'cur', 'new' and 'tmp' directories within the directory
>which may or may not be a maildir.

I was hoping that the provided mailbox/Maildir class had something, but
apparently not.

So I suggest fetching my cs.mailutils module from PyPI and using its
ismaildir(path) function, which does exactly what you describe.

Cheers,
Cameron Simpson <c...@cskk.id.au>

Chris Green

unread,
Jan 23, 2022, 4:18:22 AM1/23/22
to
Cameron Simpson <c...@cskk.id.au> wrote:
> On 22Jan2022 21:26, Chris Green <c...@isbd.net> wrote:
> >So I need to test whether a point I have reached in the hierarchy is a
> >maildir mailbox or not. Using mbox format it's easy because 'folders'
> >are directories and mailboxes are files. However with maildir the
> >'folders' have directories within them so the simple tree walking goes
> >down a level too far and finds 'folders' which aren't mailboxes called
> >'cur', 'new' and 'tmp'.
> >
> >Is there any 'ready made' way in python to tell whether a directory is
> >a maildir mailbox? If not I suppose I'll simply have to check if
> >there are 'cur', 'new' and 'tmp' directories within the directory
> >which may or may not be a maildir.
>
> I was hoping that the provided mailbox/Maildir class had something, but
> apparently not.
>
So was I! :-) It seems strange that something isn't provided as it's
rather fundamental to handling maildir.


> So I suggest fetching my cs.mailutils module from PyPI and using its
> ismaildir(path) function, which does exactly what you describe.
>
Thanks Cameron.

--
Chris Green
·

Barry Scott

unread,
Jan 23, 2022, 7:20:58 AM1/23/22
to
You do not need to walk the tree.

The structure is
Maildir/cur, new, tmp - The INBOX
Maildir/<encoded-mail-folder>/cur, new, tmp

The encoding prefixed with a "." and uses a "." between folder name parts.

An example from my Maildir is ".Python.Users/" for a folder that client
shows as Python/Users

You can see this by doing:

ls -a ~/Maildir

Barry




>
> --
> Chris Green
> ·
> --
> https://mail.python.org/mailman/listinfo/python-list

Chris Green

unread,
Jan 23, 2022, 8:03:21 AM1/23/22
to
You're talking about one specific (and IMHO not very good) way of
creating a maildir hierarchy. Here's what I see on my system:-

chris@esprimo$ cd mail
/home/chris/mail
chris@esprimo$ ls -a
. .. Gm In Ju Li Tm folder
chris@esprimo$ ls -a folder
. apexLodge entertainment greville houseHome maxMum reshapers tractor
.. archive family hardware internet money riding travel
Scans boating france health isbd pcc software ukraa
Shopping buySellAdmin friends holidays jrml personal telecoms vehicles
chris@esprimo$ ls -a folder/Shopping
. cabin cycling garden maxine pets stationery
.. car diy giftsEtc maxmum photography telecoms
apexLodge clothesEtc electrical groupon medicine riding toolstation
boat computer electronics houseHome motorcycle screwfix
books cpc food jrml music sportsEtc

No dot prefixes and my maildir hierarchy is a *real* directory hierarchy so
all the other file management utilities on my system can be used easily.



--
Chris Green
·

Barry

unread,
Jan 23, 2022, 4:34:26 PM1/23/22
to


> On 23 Jan 2022, at 17:08, Chris Green <c...@isbd.net> wrote:
Oh right! That is imposed by dovecot that I use for the imap service.

Barry

> chris@esprimo$ cd mail
> /home/chris/mail
> chris@esprimo$ ls -a
> . .. Gm In Ju Li Tm folder
> chris@esprimo$ ls -a folder
> . apexLodge entertainment greville houseHome maxMum reshapers tractor
> .. archive family hardware internet money riding travel
> Scans boating france health isbd pcc software ukraa
> Shopping buySellAdmin friends holidays jrml personal telecoms vehicles
> chris@esprimo$ ls -a folder/Shopping
> . cabin cycling garden maxine pets stationery
> .. car diy giftsEtc maxmum photography telecoms
> apexLodge clothesEtc electrical groupon medicine riding toolstation
> boat computer electronics houseHome motorcycle screwfix
> books cpc food jrml music sportsEtc
>
> No dot prefixes and my maildir hierarchy is a *real* directory hierarchy so
> all the other file management utilities on my system can be used easily.
>
>
>
> --
> Chris Green
> ·
> --
> https://mail.python.org/mailman/listinfo/python-list

0 new messages