从 python 中的 Maildir 获取所有新消息

发布于 2024-08-29 18:54:15 字数 1259 浏览 11 评论 0原文

我有一个邮件目录:

foo@foo:~/Maildir$ ls -l
total 288
drwx------ 2 foo foo 155648 2010-04-19 15:19 cur
-rw------- 1 foo foo    440 2010-03-20 08:50 dovecot.index.log
-rw------- 1 foo foo    112 2010-03-20 08:49 dovecot-uidlist
-rw------- 1 foo foo      8 2010-03-20 08:49 dovecot-uidvalidity
-rw------- 1 foo foo      0 2010-03-20 08:49 dovecot-uidvalidity.4ba48c0e
drwx------ 2 foo foo 114688 2010-04-19 16:07 new
drwx------ 2 foo foo   4096 2010-04-19 16:07 tmp

在 python 中,我试图获取所有新消息(Python 2.6.5rc2)。首先,获取“Maildir”有效:

>>> import mailbox
>>> md = mailbox.Maildir('/home/foo/Maildir')
>>> md.iterkeys().next()
'1269924477.Vfc01I4249fM708004.foo'

但是如何访问“Maildir/new”?这不起作用:

>>> md = mailbox.Maildir('/home/foo/Maildir/new')
>>> md.iterkeys().next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/mailbox.py", line 346, in iterkeys
    self._refresh()
  File "/usr/lib/python2.6/mailbox.py", line 467, in _refresh
    for entry in os.listdir(subdir_path):
OSError: [Errno 2] No such file or directory: '/home/foo/Maildir/new/new'
>>>

有什么想法吗?

I have a mail dir:

foo@foo:~/Maildir$ ls -l
total 288
drwx------ 2 foo foo 155648 2010-04-19 15:19 cur
-rw------- 1 foo foo    440 2010-03-20 08:50 dovecot.index.log
-rw------- 1 foo foo    112 2010-03-20 08:49 dovecot-uidlist
-rw------- 1 foo foo      8 2010-03-20 08:49 dovecot-uidvalidity
-rw------- 1 foo foo      0 2010-03-20 08:49 dovecot-uidvalidity.4ba48c0e
drwx------ 2 foo foo 114688 2010-04-19 16:07 new
drwx------ 2 foo foo   4096 2010-04-19 16:07 tmp

And in python I'm trying to get all new messages (Python 2.6.5rc2). First, getting "Maildir" works:

>>> import mailbox
>>> md = mailbox.Maildir('/home/foo/Maildir')
>>> md.iterkeys().next()
'1269924477.Vfc01I4249fM708004.foo'

But how do I access "Maildir/new"? This does not work:

>>> md = mailbox.Maildir('/home/foo/Maildir/new')
>>> md.iterkeys().next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/mailbox.py", line 346, in iterkeys
    self._refresh()
  File "/usr/lib/python2.6/mailbox.py", line 467, in _refresh
    for entry in os.listdir(subdir_path):
OSError: [Errno 2] No such file or directory: '/home/foo/Maildir/new/new'
>>>

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以往的大感动 2024-09-05 18:54:16

文件夹 /home/foo/Maildir/new 不是 Maildir,它是 maildir 的一部分。如果您想使用mailbox.Maildir,您需要忽略属于规范一部分的子目录和文件。否则,您根本不会将其视为 Maildir。

Maildir 模块应该newcur读取消息,并且可以选择将消息从new移动到cur 当您 close()flush() 时。要了解此实现是如何实现的,您必须查看代码。

参考文献:

The folder /home/foo/Maildir/new is not a Maildir, it is part of the maildir. If you want to use mailbox.Maildir, you need to ignore the subdirectories and files which are part of the spec. Otherwise, you will not be treating it as a Maildir at all.

The Maildir module should read messages from new and cur, and may optionally move messages from new to cur when you close() or flush(). To know how this implementation does it, you will have to look at the code.

References:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文