Distinguishing between maildir, mbox, and MH files/directories?

Tim Chase python.list at tim.thechases.com
Sun Aug 31 14:45:25 EDT 2014


Tinkering around with a little script, I found myself with the need
to walk a directory tree and process mail messaged found within.
Sometimes these end up being mbox files (with multiple messages
within), sometimes it's a Maildir structure with messages in each
individual file and extra holding directories, and sometimes it's a
MH directory.  To complicate matters, there's also the possibility of
non-{mbox,maildir,mh) files such as binary MUA caches appearing
alongside these messages.

Python knows how to handle each just fine as long as I tell it what
type of file to expect.  But is there a straight-forward way to
distinguish them?  (FWIW, the *nix "file" utility is just reporting
"ASCII text", sometimes "with very long lines", and sometimes
erroneously flags them as C or C++ files‽).

All I need is "is it maildir, mbox, mh, or something else" (I don't
have to get more complex for the "something else") inside an os.walk
loop.

Thanks for any suggestions,

-tkc






More information about the Python-list mailing list