File read and writing in binary mode...

Diez B. Roggisch deets at nospam.web.de
Wed Jun 14 08:00:39 EDT 2006


nicolasg at gmail.com schrieb:
> 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.

Now why do you think there is a distinction between binary and text 
files? Precisely because of what you observe: a text file will undergo a 
automatice file ending conversion. That means that newlines get 
translated to DOS-newlines (actually two characters) - and that makes a 
binary file corrupted.

http://zephyrfalcon.org/labs/python_pitfalls.html

Solution: only use binary files, and do the newline-translation yourself 
if needed.

Diez



More information about the Python-list mailing list