how to get size of email attachment

Tim Williams (gmail) tdwdotnet at gmail.com
Mon Aug 30 17:08:42 EDT 2004


On 30 Aug 2004 12:05:18 -0700, Joh <joh12005 at yahoo.fr> wrote:
> hello,
> 
> i'm looking for a way to get total size of an email (with its attached
> files) using library such as poplib, or ?
> 
> can someone help me or give me an url from where to start ?

thinking aloud and off the top of my head , you could try the email
module (python docs)

import email

msg = email.message_from_file(xxx) (or email.message_from_string(xxx) )

then something like 

for part in msg.walk():  
     length =  len(str(part))
     fname = part.get_filename()
or

if msg.is_multipart( ):
    temp_payload = msg.get_payload()
    for x in range(len(temp_payload)):
        print temp_payload[x].get_content_type()
        print temp_payload[x].get_filename()
        print len(str(temp_payload[x]))

Remember any size you get  will be the size of the attachment whilst
its encoded into a mime object,  which won't be the same as the size
of the file on disk.

I'm not sure the above will work,  but its in the right ball park,  If
you don't have any luck post back and I'll look into it next time I'm
in the office and have python docs easily to hand.



More information about the Python-list mailing list