MIME coding problems

sennomo at my-deja.com sennomo at my-deja.com
Thu Jul 15 12:53:45 EDT 1999


I am having problems with MIME encoding and decoding.  The following
code is supposed to take a binary file, base64-encode it, then decode
the encoded form, producing a copy of the original file.  Can anyone
explain to me why it doesn't work?

------------------------------------------
import mimetools

# open a binary file for reading
input_file = open("any.gif", 'r')

# open a transition file for writing
# to store base64 encoded form of input_file
transition_file = open("transition.gif", 'w')

# encode
mimetools.encode(input_file, transition_file, 'base64')

# close files
transition_file.close()
input_file.close()

# open encoded transition file for reading
transition_file = open("transition.gif", 'r')

# open new file for writing
# to store decoded version of transition_file
final_file = open("new.gif", 'w')

# decode, essentially creating a new copy of
# input_file
mimetools.decode(transition_file, final_file, 'base64')

# close files
final_file.close()
transition_file.close()
------------------------------------------

For some reason, "new.gif" is always shorter than the
original "any.gif".  I've tried it with different files, and changing
the order of file openings and closings, but the results are always the
same.  I am using WinNT Server 4.0.  Could that have anything to do
with the problem?  I'm kind of in a hurry to resolve this, so I would
greatly appreciate any help I can get.

sennomo


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




More information about the Python-list mailing list