[Tutor] Problems understanding semantics of readlines()

Rogério Brito linuxconsult@yahoo.com.br
Tue, 9 Apr 2002 04:09:58 -0300


	Dear people,

	I'm a newbie in Python and I'm trying to read the Guido's
	tutorial.

	So far, things have been ok and I'm quite excited with
	learning Python, but now, at chapter 7 (on input/output), I
	have a problem. I'm trying to understand the exact semantics
	of readlines() (note the plural), but the description doesn't
	seem to match what the method does.

	More specifically, I am interested to know what readlines()
	should do when called with an argument. Let's suppose that f
	is an open file object. I thought that given an integer n,
	f.readlines(n) would return a list of lines (from the current
	point in the file f) with total length <= n and which were all
	terminated with "\n".

	following interactive session shows:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
dumont:/home/rbrito> python2.2
Python 2.2.1c1 (#1, Mar 15 2002, 08:13:47) 
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open("/tmp/file.txt", "w+")
>>> f.write("a"*128*1024+"\n"+"b"*10)
>>> f.seek(0)
>>> f.readlines(15)
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(...)
aaaa\n', 'bbbbbbbbbb']
>>> f.close()
>>> 
dumont:/home/rbrito>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	I would expect it to show me just a list with zero or one
	element (BTW, which one is correct? The documentation is a bit
	confusing here, and I can interpret it both ways), but not two
	elements.

	The strange thing here is that Jython behaves differently than
	Cpython with the exact steps above: Jython 2.1 w/ j2sdk 1.3.1
	just gives me a list with one element (the first line, with
	a's), as I would expect.

	So, I'm confused here. Can anybody help this poor newbie?


	Thanks in advance for any help, Roger...

P.S.: I'm sorry if this is a FAQ.