How to get key values when iterating a mailbox?

tinnews at isbd.co.uk tinnews at isbd.co.uk
Mon Oct 18 11:42:12 EDT 2010


I'm trying to delete some messages from a mailbox when they are older
than a certain number of days. 

If I iterate through the mailbox and find a message that needs
deleting how do I get its key so I can do "remove(key)"?

The trouble is that, as the documentation says: "The default Mailbox
iterator iterates over message representations, not keys as the
default dictionary iterator does." So if you try something like:-

    for f in os.listdir(junkdir):
        mbxPath = os.path.join(junkdir, f)
        mbx = mailbox.mbox(mbxPath, factory=None)                 
        mbx.lock()  
        for k, msg in mbx:  
            if <something is true>
                msg.remove(k)
        mbx.flush() 
        mbx.unlock() 

Then you get an exception on "for k, msg in mbx" which is an attribute
error, presumably because a mailbox isn't a 'real' iterator. So how,
do I get that key value?

-- 
Chris Green



More information about the Python-list mailing list