Problem reading/writing files

smeenehan at hmc.edu smeenehan at hmc.edu
Fri Aug 4 07:59:57 EDT 2006


> What platform? What version of Python? Have you opened the
> file in binary mode  i.e. open('thefile', 'rb') ?? Show us the relevant
> parts of your code, plus what  caused you to conclude that read()
> changed data on the fly in an undocumented fashion.

Yes, I've been reading and writing everything in binary mode. I'm using
version 2.4 on a Windows XP machine.

Here is the code that I have been using to split up the original file:

f = open('evil2.gfx','rb')
i1 = open('img1.jpg','wb')
i2 = open('img2.png','wb')
i3 = open('img3.gif','wb')
i4 = open('img4.png','wb')
i5 = open('img5.jpg','wb')


for i in range(0,67575,5):
    i1.write(f.read(1))
    i2.write(f.read(1))
    i3.write(f.read(1))
    i4.write(f.read(1))
    i5.write(f.read(1))

f.close()
i1.close()
i2.close()
i3.close()
i4.close()
i5.close()

I first noticed the problem by looking at the original file and
img1.jpg side by side with a hex editor. Since img1 contains every 5th
byte from the original file, I was able to find many places where \x00
should have been copied to img1.jpg, but instead a \x20 was copied.
What caused me to suspect the read method was the following:

>>> f = open('evil2.gfx','rb')
>>> s = f.read()
print repr(s[19:22])
'\xe0 \r'

Now, I have checked many times with a hex editor that the 21st byte of
the file is \x00, yet above you can see that it is reading it as a
space. I've repeated this with several different nulls in the original
file and the result is always the same.

As I said in my original post, when I try simply writing a null to my
own file and reading it (as someone mentioned earlier) everything is
fine. It seems to be only this file which is causing issue.




More information about the Python-list mailing list