reading from sys.stdin

Steve Holden steve at holdenweb.com
Thu Apr 12 15:39:51 CEST 2007


Maric Michaud wrote:
> Le jeudi 12 avril 2007 10:34, Diez B. Roggisch a écrit :
>> I presume this is an OS thing. The first lines aren't communicated to
>> the process until either the file is closed - C-d - or the buffer the OS
>> puts before the stream is filled. You can switch to unbuffered behviour
>> somehow, google for it. Termios should be in your query.
> 
> I don't know if this a python or OS thing, but I know that iterating over a 
> file is not like applying successive call to readline method. You should try 
> to use readline instead.
> 
That, of course, is because files are iterators.

> The following work exactly the same on windows and Linux (python2.4) :
> 
>>>> f=open('txt')
>>>> l=f.readline()
>>>> while(l) :
> ...  print l,
> ...  print "rest : " + f.read()
> ...  l=f.readline()
> ...
> foo
> rest : bar
> baz
> 
> works as expected, while :
> 
>>>> f=open('txt')
>>>> for l in f :
> ...  print l,
> ...  print "rest : " + f.read()
> ...
> foo
> rest :
> bar
> rest :
> baz
> rest :
> 
> doesn't, it seems that file iteratiion itself use a buffer. In python 2.5, you 
> just can't do this :
> 
> Python 2.5 (release25-maint, Dec  9 2006, 14:35:53)
> [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-20)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> f=open('txt')
>>>> for l in f :
> ...  print l,
> ...  print "rest : " + f.read()
> ...
> foo
> Traceback (most recent call last):
>   File "<stdin>", line 3, in <module>
> ValueError: Mixing iteration and read methods would lose data
> 
But you *can* do this (untested):

for l in f:
   print l,
   print "rest:", f.next()

Of course there are always edge cases. In this particular instance I 
suspect that the handling of files with odd numbers of lines might be 
slightly different, but I can't work up enough enthusiasm to actually 
run this code. The error method pretty much explains what the problem 
is: you have to iterate over files or read them, you shouldn't try and 
do both on the same file.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list