[Python-bugs-list] [ python-Bugs-571568 ] binary file read/write inconsistency

noreply@sourceforge.net noreply@sourceforge.net
Thu, 20 Jun 2002 06:48:29 -0700


Bugs item #571568, was opened at 2002-06-20 05:56
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=571568&group_id=5470

Category: Windows
Group: None
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Anatoly Artamonov (arthem)
Assigned to: Michael Hudson (mwh)
Summary: binary file read/write inconsistency

Initial Comment:
I'm using py 2.1.3 for windows
here's a sample code that works fine in Unix and somekind 
weird in Win32:
###############################
import os.path

pth = "22"
f = open(pth,'w')
for k in range(256):  
    f.write(chr(k))
f.close()

f = open(pth)
dat = f.read()
f.close()
print len(dat), 'vs' , os.path.getsize(pth)
print "last read symbol is: 0x%x" % ord(dat[-1])
#################################

Windows version writes 0x0D 0x0A instead of 0x0A so file 
length is 257 bytes instead of 256 as one might expect
and reading just ends at 0x19 (probably end of file 
symbol). 


----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2002-06-20 09:48

Message:
Logged In: YES 
user_id=31435

Change your open() line to

f = open(pth, 'w'b')

and the test case should work the same across platforms.  
chr(26) is also an end-of-file symbol in text mode on 
Windows.  Opening in binary mode is again the cure.

----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2002-06-20 08:15

Message:
Logged In: YES 
user_id=6656

Files are opened in text mode by default.

You can question this, but it ain't gonna change.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=571568&group_id=5470