[Python] Python and parsing email

Rob Hudson rob at euglug.net
Tue Apr 4 14:45:08 PDT 2006


I'm using the email built-in to parse email messages looking for 
attachments.  I've got it working but I wanted to continue and make this 
thing smarter.

I've got this basic structure:

  61     for part in msg.walk():
  62         # multipart/* are just containers
  63         if part.get_content_maintype() == 'multipart':
  64             continue
  65         filename = part.get_filename()
  66         # If we have a filename, we have an attachment
  67         if filename:
  68             if filename.endswith(".xls"):
  69                 # Looks like a spreadsheet
  70                 fp = open(os.path.join(dir, filename), 'wb')
  71                 fp.write(part.get_payload(decode=1))
  72                 fp.close()

What I want to change is instead of writing the file with the given 
filename of the attachment, I want to write the filename using the email 
address name and a timestamp.

To figure out how to do this, I was using the Python shell and walking 
through this same sequence.  I open an example email using:

   msg = email.message_from_file(fh)

But when I do:

   part = msg.walk()

I get a generator object not the object I'm looking for.  How do I get 
the object to which I can call "get_content_maintype()" so I can keep 
testing and playing with the methods available to me.  I'm mostly just 
walking through the process looking and doing a dir() on each object to 
see the methods available until I find what I'm looking for.

Thanks,
Rob


More information about the Python mailing list