Skip to main content
  1. Posts/

Batteries Included (or, Maildir to mbox, again)

UPDATE 7 June 2010: Added usage information to docstring.

UPDATE 30 January 2012: Frédéric Grosshans has provided an updated version that supports nested maildirs; you can find it at github . YMMV.

My script for converting maildir to mbox continues to be one of the most popular pages on (according to Google Analytics). Of course, even after I updated it slightly in February, it still had a couple of bugs, likely introduced when I converted the page from MoinMoin to WordPress. This afternoon I finally decided to clear out the pending comments about those bugs, and update it.

While looking at the Python documentation for the mailbox` <<> >\`_ package included in the standard library, I realized it could probably be simplified even further by using the library’s native mbox <<> >\_ support. I’m also more comfortable using the standard library’s implementation of mbox rather than my hacked up raw file implementation (who knows, the standard library may do exactly what my script did: I’m not an mbox expert by any stretch of the imagination).

The new script is below. I should note that it’s received very little testing, and I make no guarantees. I also should note that there is nothing creative or original about this. It just uses Python’s excellent standard library. As they say, “batteries included”.

/del> coding: utf-8

“”“maildir2mbox.pyNathan R. Yergler, 6 June 2010

This file does not contain sufficient creative expression to invokeassertion of copyright.  No warranty is expressed or implied; use atyour own risk.


Uses Python’s included mailbox library to convert mail archives frommaildir [] to mbox [] format.

See for full documentation on this library.


To run, save as and run:

$ python [maildir_path] [mbox_filename]

[maildir_path] should be the the path to the actual maildir (containing new, cur, tmp);

[mbox_filename] will be newly created.“”“

import mailboximport sysimport email

    open the existing maildir and the target mbox file
maildir = mailbox.Maildir(sys.argv [-2], email.message_from_file)mbox = mailbox.mbox(sys.argv[-1])

    lock the mbox

    iterate over messages in the maildir and add to the mbox
for msg in maildir:

    close and unlock