Problems decoding and writing mail attachment

Discussion in 'Python' started by edwinek, Aug 4, 2003.

  1. edwinek

    edwinek Guest

    I've written a script which I use to save attached binary files from
    e-mails to disk. The core is simply:

    decodedfile = bas64.decodestring(attachment)
    fle = open('image.jpg','wb')
    fle.write(decodedfile)
    fle.close

    However, the files written to disk are regularly incomplete, i.e.
    jpg's having a grey bar at the bottom, gif's don't even display, pdf's
    give errors.
    What am I doing wrong here?

    Thanks,
    Edwin
    edwinek, Aug 4, 2003
    #1
    1. Advertising

  2. edwinek wrote:

    > I've written a script which I use to save attached binary files from
    > e-mails to disk. The core is simply:
    >
    > decodedfile = bas64.decodestring(attachment)
    > fle = open('image.jpg','wb')
    > fle.write(decodedfile)
    > fle.close
    >
    > However, the files written to disk are regularly incomplete, i.e.
    > jpg's having a grey bar at the bottom, gif's don't even display, pdf's
    > give errors.
    > What am I doing wrong here?


    You're not calling fle.close -- you lack parentheses after the
    method, so you're just mentioning it, NOT calling it. Not sure
    if that's your problem (seems unlikely), but it most definitely
    IS something you're doing wrong, so I thought I'd point it out.


    Alex
    Alex Martelli, Aug 4, 2003
    #2
    1. Advertising

  3. "edwinek" <> wrote:

    > I've written a script which I use to save attached binary files from
    > e-mails to disk. The core is simply:
    >
    > decodedfile = bas64.decodestring(attachment)
    > fle = open('image.jpg','wb')
    > fle.write(decodedfile)
    > fle.close
    >
    > However, the files written to disk are regularly incomplete, i.e.
    > jpg's having a grey bar at the bottom, gif's don't even display, pdf's
    > give errors.


    > What am I doing wrong here?


    chances are that you're not closing the file before using it: try changing
    the last line to

    fle.close()

    </F>
    Fredrik Lundh, Aug 4, 2003
    #3
    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. Jonas Galvez

    Decoding 'funky' e-mail subjects

    Jonas Galvez, Jun 7, 2004, in forum: Python
    Replies:
    7
    Views:
    418
    Michel Claveau/Hamster
    Jun 8, 2004
  2. Kyle Pula

    CDONTS - PDF Attachment / decoding problem

    Kyle Pula, Jul 25, 2003, in forum: ASP General
    Replies:
    2
    Views:
    133
    Kyle Pula
    Jul 28, 2003
  3. ty

    send e-mail and attachment

    ty, Oct 15, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    95
    Tintin
    Oct 15, 2003
  4. Alla
    Replies:
    2
    Views:
    99
    Tad McClellan
    Feb 17, 2006
  5. Arvin Portlock
    Replies:
    0
    Views:
    189
    Arvin Portlock
    May 3, 2006
Loading...

Share This Page