mailbox --- 以各種格式操作郵件信箱¶
原始碼:Lib/mailbox.py
This module defines two classes, Mailbox and Message, for
accessing and manipulating on-disk mailboxes and the messages they contain.
Mailbox offers a dictionary-like mapping from keys to messages.
Message extends the email.message module's
Message class with format-specific state and behavior.
Supported mailbox formats are Maildir, mbox, MH, Babyl, and MMDF.
也參考
email模組Represent and manipulate messages.
Mailbox 物件¶
- class mailbox.Mailbox¶
A mailbox, which may be inspected and modified.
The
Mailboxclass defines an interface and is not intended to be instantiated. Instead, format-specific subclasses should inherit fromMailboxand your code should instantiate a particular subclass.The
Mailboxinterface is dictionary-like, with small keys corresponding to messages. Keys are issued by theMailboxinstance with which they will be used and are only meaningful to thatMailboxinstance. A key continues to identify a message even if the corresponding message is modified, such as by replacing it with another message.Messages may be added to a
Mailboxinstance using the set-like methodadd()and removed using adelstatement or the set-like methodsremove()anddiscard().Mailboxinterface semantics differ from dictionary semantics in some noteworthy ways. Each time a message is requested, a new representation (typically aMessageinstance) is generated based upon the current state of the mailbox. Similarly, when a message is added to aMailboxinstance, the provided message representation's contents are copied. In neither case is a reference to the message representation kept by theMailboxinstance.The default
Mailboxiterator iterates over message representations, not keys as the defaultdictionaryiterator does. Moreover, modification of a mailbox during iteration is safe and well-defined. Messages added to the mailbox after an iterator is created will not be seen by the iterator. Messages removed from the mailbox before the iterator yields them will be silently skipped, though using a key from an iterator may result in aKeyErrorexception if the corresponding message is subsequently removed.警告
Be very cautious when modifying mailboxes that might be simultaneously changed by some other process. The safest mailbox format to use for such tasks is
Maildir; try to avoid using single-file formats such asmboxfor concurrent writing. If you're modifying a mailbox, you must lock it by calling thelock()andunlock()methods before reading any messages in the file or making any changes by adding or deleting a message. Failing to lock the mailbox runs the risk of losing messages or corrupting the entire mailbox.Mailboxinstances have the following methods:- add(message)¶
Add message to the mailbox and return the key that has been assigned to it.
Parameter message may be a
Messageinstance, anemail.message.Messageinstance, a string, a byte string, or a file-like object (which should be open in binary mode). If message is an instance of the appropriate format-specificMessagesubclass (e.g., if it's anmboxMessageinstance and this is anmboxinstance), its format-specific information is used. Otherwise, reasonable defaults for format-specific information are used.在 3.2 版的變更: Support for binary input was added.
- remove(key)¶
- __delitem__(key)¶
- discard(key)¶
Delete the message corresponding to key from the mailbox.
If no such message exists, a
KeyErrorexception is raised if the method was called asremove()or__delitem__()but no exception is raised if the method was called asdiscard(). The behavior ofdiscard()may be preferred if the underlying mailbox format supports concurrent modification by other processes.
- __setitem__(key, message)¶
Replace the message corresponding to key with message. Raise a
KeyErrorexception if no message already corresponds to key.As with
add(), parameter message may be aMessageinstance, anemail.message.Messageinstance, a string, a byte string, or a file-like object (which should be open in binary mode). If message is an instance of the appropriate format-specificMessagesubclass (e.g., if it's anmboxMessageinstance and this is anmboxinstance), its format-specific information is used. Otherwise, the format-specific information of the message that currently corresponds to key is left unchanged.
- iterkeys()¶