Can I get message filename from a Maildir mailbox stream?

Discussion in 'Python' started by noah@noah.org, Feb 19, 2005.

  1. Guest

    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
     
    , Feb 19, 2005
    #1
    1. Advertising

  2. Phil Jackson Guest

    writes:

    > 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
     
    Phil Jackson, Feb 19, 2005
    #2
    1. Advertising

  3. >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
     
    Michael Josephson, Feb 19, 2005
    #3
  4. Noah Guest

    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
     
    Noah, Feb 22, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matej Cepl

    mailbox.py examples (Maildir)

    Matej Cepl, Aug 6, 2003, in forum: Python
    Replies:
    1
    Views:
    482
    David M. Cooke
    Aug 6, 2003
  2. Replies:
    0
    Views:
    358
  3. Replies:
    2
    Views:
    430
  4. Replies:
    0
    Views:
    409
  5. Replies:
    2
    Views:
    307
Loading...

Share This Page