Problem reading/writing files

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Fri Aug 4 10:06:10 EDT 2006


smeenehan at hmc.edu schreef:
> 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.

Very weird. I tried your code on my system (Python 2.4, Windows XP) (but 
using a copy of evil2.gfx I still had on my system), with no problems.

Are you sure that you don't have 2 copies of that file around, and that 
your program is using the wrong one? Or is it possible that some module 
imported with 'from blabla import *' clashes with the builtin open()?

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven



More information about the Python-list mailing list