Reading from text files

Thomas Philips tkpmep at hotmail.com
Sun Feb 22 21:13:12 CET 2004


In the course of playing around with file input and output, I came
across some behavior that is not quite intuitive. I created a simple
text file, test.txt, which contains only 3 lines, and which I expect
will have 5 characters (the digits 1, 2, and 3, and two newline
characters, the first after 1 and the second after 2). Here it is in
all its glory:
1
2
3

However, when I read it using open()and then view it using
>>> file.seek(0); file.read(); file.tell()
I get:
'1\n2\n3'
7L

Python thinks there are 7 characters in the file! If I type 
>>> file.seek(1); file.read() OR >>>file.seek(2); file.read()
I get
'\n2\n3'

but 
>>> file.seek(3); file.read()
gives me what I expected to get with file.seek(2); file.read()
'2\n3'

It appears that Python sometimes counts each of the newline escape
sequences as 2 separate characters and at other times as 1 indivisible
character. What is the appropriate way to think about these
characters?

Thomas Philips



More information about the Python-list mailing list