Can I get message filename from a Maildir mailbox stream?

N

noah

Is there a way to figure out what
filename an email object points to in a
qmail style Maildir directory?

Hmmm... I don't think so, but I'm hoping I wrong.

I instantiated a Maildir mailbox and I'm
iterating through the messages. When I find a
special message I want to move it or delete it
or something.

Yours,
Noah
 
P

Phil Jackson

Is there a way to figure out what filename an email object points to
in a qmail style Maildir directory?

What do you mean bu "email object"? Surely if you are iterating a
Maildir then you should have the filename. Or am I misunderstanding
what you mean?

Phil
 
M

Michael Josephson

a way to figure out what filename an email object points to in a
qmail style Maildir directory?

Assuming you're using the mailbox module from the standard Python
library, the Message objects you get back provide access to the file
object as an instance variable called fp. So if you have:

maildir = mailbox.Maildir("/home/mike/Maildir")
message = maildir.next()

The filename is message.fp.name

Note that the file object is open when you are in this state so you
may want to close it before you do a move/delete etc.

-Michael
 
N

Noah

This didn't work. I'm using the standard Python 2.3.4 library
mailbox.Maildir. I got an error that message instance
has no attribute 'fp'.

I ended up not using mailbox.Maildir at all.
It occured to me that since Maildir style mailboxes
is just a collection of files that it was simpler to write
a generator that walked the directory using os.listdir.

def Maildir_messages (path):
d = os.listdir(path)
for filename in d:
fin = file (os.path.join(path, filename))
yield (email.message_from_file(fin), filename)
fin.close()

for msg, msg_filename in Maildir_messages ("/home/noah/Maildir/new"):
print msg_filename
print msg['Subject']

Yours,
Noah
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top