reading from sys.stdin

Maric Michaud maric at aristote.info
Thu Apr 12 14:46:12 CEST 2007


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.

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

-- 
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 4 26 88 00 97
Mobile: +33 6 32 77 00 21



More information about the Python-list mailing list