File read and writing in binary mode...

nicolasg at gmail.com nicolasg at gmail.com
Wed Jun 14 13:55:03 CEST 2006


Hi,

I'm trying to open a file (any file) in binary mode and save it inside
a new text file.
After that I want to read the source from the text file and save it
back to the disk with its original form. The problem is tha the binary
source that I extract from the text file seems to be diferent from the
source I saved. Here is my code:
1)
handle=file('image.gif','rb')
source=handle.read()
handle.close()

if I save the file directly everything is well :
2A)
handle=file('imageDuplicated.gif','wb')
handle.write(source)
handle.close()

the file imageDuplicated.gif will be exactly the same as the original
image.gif.
But if I save the source to a text file I have porblem :
2B)
handle=file('text.txt','w')
handle.write(source)
handle.close()

handle=file('text.txt','r')
source2=handle.read()
handle.close()

handle=file('imageDuplicated.gif','wb')
handle.write(source2)
handle.close()

the files are completly different and I even cant display the image
from the imageDuplicated.gif .

something changes when I save the source in the text file because in
2B) source == source2 returns a False .
I suspect that maybe the encoding is making a conflict but I don't know
how to manipulate it...
Every help is welcome, thanks.




More information about the Python-list mailing list