readline()/readlines() inconsistent?

John W. Baxter jwbaxter at spamcop.com
Sun Jul 8 12:49:55 EDT 2001


In article <3B482C44.6E6EB163 at snakefarm.org>, Carsten Gaebler
<clpy at snakefarm.org> wrote:

> Hi there!
> 
> I've just come across a strange difference between readline() and
> readlines():
> 
> Python 2.1 (#17, Jun 15 2001, 15:24:30) 
> [GCC 2.95.2 19991024 (release)] on linux2
> Type "copyright", "credits" or "license" for more information.
> >>> f = open("foobar", "w")
> >>> f.readline()
> ''
> >>> f.close()
> >>> 
> >>> f =  open("foobar", "w")
> >>> f.readlines()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> IOError: [Errno 9] Bad file descriptor
> >>> 
> 
> 
> Why does readlines() raise an exception while readline() does not? I
> expected it to return an empty list. According to the docs readlines()
> uses readline(). If I open the file "w+" everything's fine. Opinions?
> 
> Regards
> Carsten.

Interesting...

That's not the result I get on the handiest Python 2.1 (on the Mac OS X
machine on which I'm reading the newsgroup).  But it *is* the result I
get on a Red Hat machine from HP, whose Linux describes itself as:

$ cat /proc/version
Linux version 2.2.14-5.0smp (root at porky.devel.redhat.com) (gcc version
egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)) #1 SMP Tue Mar 7
21:01:40 EST 2000


The Mac OS X run is here:

Python 2.1 (#1, 04/22/01, 11:06:25) 
[GCC Apple DevKit-based CPP 6.0alpha] on darwin1
Type "copyright", "credits" or "license" for more information.
>>> f = open ("spam", "w")
>>> f.readline()
''
>>> f.readline()
''
>>> f.close()
>>> f = open ("spam", "w")
>>> f.readlines()
[]
>>> f.close()

   --John



More information about the Python-list mailing list