chr(26) => file reading bug

Dennis E. Hamilton infonuovo at email.com
Tue Mar 28 13:05:33 EST 2000


You must open the file as binary, not as text.  You will then be able to
read the complete file as an octet-sequence of the size recorded in the file
system.  To be safe, you might want to write your test file in binary mode
too.

I can't tell from the library documentation whether f.readlines() will still
work properly and only notice the true EOF, and also deal with CR/LF
translation to '/n' for you.  Since there is a tendency for obvious things
to still operate in obvious ways in Python, I would expect it to work.

I created your example as file chr26.py and changed the opens to use modes
'wb' and 'rb'.  I then used print t.  It worked fine on Python 1.5.2 running
on Windows 98 SE.

-- Dennis

PS: For general information, Chr(26) is the "CTRL-Z" code of ASCII/ISO646
keyboards, a very old convention for terminating text files (as in the
Python interactive mode) when file systems did not keep sizes to exact
bytes, storing (logical) sectors instead.

-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of Michal Bozon
Sent: Tuesday, March 28, 2000 08:47
To: python-list at python.org
Subject: chr(26) => file reading bug


When it is opened a file containing a char chr(26) (i.e. chr(0x1A)), it is
read only until this char.
e.g.:
>>> s = 'abcd'+chr(26)+'efgh'
>>> fn = 'c:/test.dat'
>>> f = open(fn, 'w')
>>> f.write(s)
>>> f.close()
>>> f = open(fn, 'r')
>>> t = f.readlines()
>>> f.close()
>>> t
['abcd']

(Maybe not exactly so)

How can it be fixed?

I use Python 1.5.2 on Win98


Thanks,
Michal Bozon
Fac.Sci., Charles Univ., Prague, Czech. Rep
- - - - -
bozon at natur.cuni.cz
http://www.natur.cuni.cz/~bozon

--
http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list