On this page
mailbox.Maildir
Prev: mailbox.Mailbox | Up: mailbox | Next: mailbox.MaildirMessage
Rel: mailbox.Babyl, mailbox.BabylMessage, mailbox.Error, mailbox.ExternalClashError
Ref: docs.python.org — mailbox.Maildir
Tags: #public #python3 #stdlib #mailbox
Import
import mailbox
from mailbox import Maildir
Signature & Overview
mailbox.Maildir(dirname, factory=None, create=True)
A qmail-style Maildir mailbox.
Methods & Attributes
Maildir.add(self, message)— Add message and return assigned key.Maildir.add_flag(self, key, flag: str)— Set the given flag(s) without changing others on the keyed message.Maildir.add_folder(self, folder)— Create a folder and return a Maildir instance representing it.Maildir.clean(self)— Delete old files in "tmp".Maildir.clear(self)— Delete all messages.Maildir.close(self)— Flush and close the mailbox.Maildir.discard(self, key)— If the keyed message exists, remove it.Maildir.flush(self)— Write any pending changes to disk.Maildir.get(self, key, default=None)— Return the keyed message, or default if it doesn't exist.Maildir.get_bytes(self, key)— Return a bytes representation or raise a KeyError.Maildir.get_file(self, key)— Return a file-like representation or raise a KeyError.Maildir.get_flags(self, key)— Return as a string the standard flags that are set on the keyed message.Maildir.get_folder(self, folder)— Return a Maildir instance for the named folder.Maildir.get_info(self, key)— Get the keyed message's "info" as a string.Maildir.get_message(self, key)— Return a Message representation or raise a KeyError.Maildir.get_string(self, key)— Return a string representation or raise a KeyError.Maildir.items(self)— Return a list of (key, message) tuples. Memory intensive.Maildir.iteritems(self)— Return an iterator over (key, message) tuples.Maildir.iterkeys(self)— Return an iterator over keys.Maildir.itervalues(self)— Return an iterator over all messages.Maildir.keys(self)— Return a list of keys.Maildir.list_folders(self)— Return a list of folder names.Maildir.lock(self)— Lock the mailbox.Maildir.next(self)— Return the next message in a one-time iteration.Maildir.pop(self, key, default=None)— Delete the keyed message and return it, or default.Maildir.popitem(self)— Delete an arbitrary (key, message) pair and return it.Maildir.remove(self, key)— Remove the keyed message; raise KeyError if it doesn't exist.Maildir.remove_flag(self, key, flag: str)— Unset the given string flag(s) without changing others on the keyed message.Maildir.remove_folder(self, folder)— Delete the named folder, which must be empty.Maildir.set_flags(self, key, flags: str)— Set the given flags and unset all others on the keyed message.Maildir.set_info(self, key, info: str)— Set the keyed message's "info" string.Maildir.unlock(self)— Unlock the mailbox if it is locked.Maildir.update(self, arg=None)— Change the messages that correspond to certain keys.Maildir.values(self)— Return a list of messages. Memory intensive.
Usage
import mailbox
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
md = mailbox.Maildir(Path(tmp) / "maildir", create=True)
msg = mailbox.MaildirMessage()
msg["Subject"] = "Ping"
key = md.add(msg)
assert md[key]["Subject"] == "Ping"
md.close()
Gotchas
- Best local format for concurrent access: Stores one file per message across
tmp/,new/,cur/without whole-file rewrites.
REPL Inspect
from mailbox import Maildir
help(Maildir)
Linked from
Related notes
- tuts/python3/stdlib_module/mailbox/Mailbox Linked here; links to this page; shared tags: #mailbox, #stdlib
- tuts/python3/stdlib_module/mailbox/MaildirMessage Linked here; links to this page; shared tags: #mailbox, #stdlib
- tuts/python3/stdlib_module/mailbox/Babyl Linked here; shared tags: #mailbox, #stdlib
- tuts/python3/stdlib_module/mailbox/BabylMessage Linked here; shared tags: #mailbox, #stdlib
- tuts/python3/stdlib_module/mailbox/Error Linked here; shared tags: #mailbox, #stdlib
- tuts/python3/stdlib_module/mailbox/ExternalClashError Linked here; shared tags: #mailbox, #stdlib
- tuts/python3/stdlib_module/mailbox links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/mailbox/FormatError shared tags: #mailbox, #stdlib
- tuts/python3/stdlib_module/mailbox/mbox shared tags: #mailbox, #stdlib
- tuts/python3/stdlib_module/mailbox/mboxMessage shared tags: #mailbox, #stdlib