从 python 中的 Maildir 获取所有新消息
我有一个邮件目录:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文件夹
/home/foo/Maildir/new
不是 Maildir,它是 maildir 的一部分。如果您想使用mailbox.Maildir,您需要忽略属于规范一部分的子目录和文件。否则,您根本不会将其视为 Maildir。Maildir 模块应该从
new
和cur
读取消息,并且可以选择将消息从new
移动到cur
当您close()
或flush()
时。要了解此实现是如何实现的,您必须查看代码。参考文献:
The folder
/home/foo/Maildir/new
is not a Maildir, it is part of the maildir. If you want to usemailbox.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
andcur
, and may optionally move messages fromnew
tocur
when youclose()
orflush()
. To know how this implementation does it, you will have to look at the code.References: