[Python-bugs-list] [ python-Bugs-816627 ] file read() forgets some bytes

SourceForge.net noreply at sourceforge.net
Thu Oct 2 12:02:04 EDT 2003


Bugs item #816627, was opened at 2003-10-02 11:54
Message generated for change (Comment added) made by tim_one
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=816627&group_id=5470

Category: Python Library
>Group: Not a Bug
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Philippe Fremy (pfremy)
Assigned to: Nobody/Anonymous (nobody)
Summary: file read() forgets some bytes

Initial Comment:
It seems the python miscalculates the size of a file.

This is on windows, with python 2.2.2 from ActiveState



E:\work\jayacard\inkit\demo>python

ActivePython 2.2.2 Build 224 (ActiveState Corp.) based on

Python 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit

(Intel)] on win32

Type "help", "copyright", "credits" or "license" for

more information.

>>> import array, os

>>> fname = "current_img.jpg"

>>> print len(open(fname).read())

41

>>> print os.stat(fname).st_size

693



As you can see, there some mismatch here. 693 is indeed

the size reported by the windows explorer.



>>> a = array.array('B')

>>> a.fromfile( open(fname), 693)

Traceback (most recent call last):

  File "<stdin>", line 1, in ?

EOFError: not enough items in file



>>> a.fromfile(open(fname), 42)

Traceback (most recent call last):

  File "<stdin>", line 1, in ?

EOFError: not enough items in file

>>> a.fromfile(open(fname), 41)

>>> a = array.array('B')

>>> a.fromfile(open(fname), 41)

>>> len(a)

41





I attach the nasty file



This is a very serious bug for me.

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

>Comment By: Tim Peters (tim_one)
Date: 2003-10-02 12:02

Message:
Logged In: YES 
user_id=31435

Windows makes a distinction between binary-mode and text-

mode files.  open() defaults to text-mode.  A .jpg is certainly 

a binary file, so on Windows you *must* open with it in binary 

mode.  That means changing your



    open(fname)



to



    open(fname, 'rb')



Your problem will go away then.  It's unfortunate, but that's 

the way Windows works -- it's not a Python bug.

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

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



More information about the Python-bugs-list mailing list