mailbox — 以各种格式操纵邮箱

源代码: Lib/mailbox.py


此模块定义 2 类, 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 模块的 Message class with format-specific state and behavior. Supported mailbox formats are Maildir, mbox, MH, Babyl, and MMDF.

另请参阅

模块 email

表示和操纵消息。

Mailbox 对象

class mailbox. Mailbox

可以审查和修改的邮箱。

The Mailbox class defines an interface and is not intended to be instantiated. Instead, format-specific subclasses should inherit from Mailbox and your code should instantiate a particular subclass.

The Mailbox interface is dictionary-like, with small keys corresponding to messages. Keys are issued by the Mailbox instance with which they will be used and are only meaningful to that Mailbox instance. 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 Mailbox instance using the set-like method add() and removed using a del statement or the set-like methods remove() and discard() .

Mailbox interface semantics differ from dictionary semantics in some noteworthy ways. Each time a message is requested, a new representation (typically a Message instance) is generated based upon the current state of the mailbox. Similarly, when a message is added to a Mailbox instance, the provided message representation’s contents are copied. In neither case is a reference to the message representation kept by the Mailbox 实例。

默认 Mailbox iterator iterates over message representations, not keys as the default dictionary iterator 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 a KeyError exception 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 as mbox for concurrent writing. If you’re modifying a mailbox, you must lock it by calling the lock() and unlock() 方法 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.

Mailbox 实例具有下列方法:

add ( message )

添加 message to the mailbox and return the key that has been assigned to it.

参数 message 可以是 Message instance, an email.message.Message instance, 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-specific Message subclass (e.g., if it’s an mboxMessage instance and this is an mbox instance), its format-specific information is used. Otherwise, reasonable defaults for format-specific information are used.

3.2 版改变: 添加支持二进制输入。

remove ( key )
__delitem__ ( key )
discard ( key )

Delete the message corresponding to key from the mailbox.

If no such message exists, a KeyError exception is raised if the method was called as remove() or __delitem__() but no exception is raised if the method was called as discard() . The behavior of discard() 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 KeyError exception if no message already corresponds to key .

就像 add() , parameter message 可以是 Message instance, an email.message.Message instance, 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-specific Message subclass (e.g., if it’s an mboxMessage instance and this is an mbox instance), its format-specific information is used. Otherwise, the format-specific information of the message that currently corresponds to key 保持不变。

iterkeys ( )

返回 iterator over all keys

keys ( )

如同 iterkeys() , except that a list is returned rather than an iterator

itervalues ( )
__iter__ ( )

返回 iterator over representations of all messages. The messages are represented as instances of the appropriate format-specific Message subclass unless a custom message factory was specified when the Mailbox instance was initialized.

注意

行为在 __iter__() is unlike that of dictionaries, which iterate over keys.

( )

如同 itervalues() , except that a list is returned rather than an iterator

iteritems ( )

返回 iterator over ( key , message ) pairs, where key is a key and message is a message representation. The messages are represented as instances of the appropriate format-specific Message subclass unless a custom message factory was specified when the Mailbox instance was initialized.

items ( )

如同 iteritems() , except that a list of pairs is returned rather than an iterator of pairs.

get ( key , default = None )
__getitem__ ( key )

Return a representation of the message corresponding to key . If no such message exists, default is returned if the method was called as get() KeyError exception is raised if the method was called as __getitem__() . The message is represented as an instance of the appropriate format-specific Message subclass unless a custom message factory was specified when the Mailbox instance was initialized.

get_message ( key )

Return a representation of the message corresponding to key as an instance of the appropriate format-specific Message subclass, or raise a KeyError exception if no such message exists.

get_bytes ( key )

Return a byte representation of the message corresponding to key , or raise a KeyError exception if no such message exists.

Added in version 3.2.