473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

reading from sys.stdin

I can't break out of the for loop in this example:

------
import sys

lst = []
for line in sys.stdin:
lst.append(line)
break

print lst
-----------

But, I can break out of the for loop when I do this:

---------
import sys

lst = []
for line in open("aaa.txt"):
lst.append(line)
break

print lst
----------

Why doesn't break work in the first example?

Apr 12 '07 #1
25 5811
7stud schrieb:
I can't break out of the for loop in this example:

------
import sys

lst = []
for line in sys.stdin:
lst.append(line)
break

print lst
-----------
Works for me. But only after the stdin is closed with a C-d.

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.

Either way, it's not python behaving differently.

Diez
Apr 12 '07 #2

On Apr 12, 2007, at 3:20 AM, 7stud wrote:
I can't break out of the for loop in this example:

------
import sys

lst = []
for line in sys.stdin:
lst.append(line)
break

print lst
-----------

But, I can break out of the for loop when I do this:

---------
import sys

lst = []
for line in open("aaa.txt"):
lst.append(line)
break

print lst
----------

Why doesn't break work in the first example?
My guess is because you've not entered an EOF. Try entering ^D (or
if you're on Windows I think it's ^Z) while using your first example
and see what it does.
Apr 12 '07 #3
En Thu, 12 Apr 2007 05:20:58 -0300, 7stud <bb**********@yahoo.com>
escribió:
I can't break out of the for loop in this example:

------
import sys

lst = []
for line in sys.stdin:
lst.append(line)
break

print lst
-----------
Python 2.5.1c1 (r251c1:54692, Apr 5 2007, 09:19:18) [MSC v.1310 32 bit
(Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
pyimport sys
pylst = []
pyfor line in sys.stdin:
.... lst.append(line)
.... break
....
hola
que
tal
como
estan
^Z
pyprint lst
['hola\n']
-----------

I typed many lines, but lst contains only one item, as expected. Same as
your regular file example: the file contains many lines, but only the
first goes into the list.

--
Gabriel Genellina

Apr 12 '07 #4
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
Apr 12 '07 #5
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

Apr 12 '07 #6
On Apr 12, 1:20 am, "7stud" <bbxx789_0...@yahoo.comwrote:
I can't break out of the for loop in this example:

------
import sys

lst = []
for line in sys.stdin:
lst.append(line)
break

print lst
-----------

But, I can break out of the for loop when I do this:

---------
import sys

lst = []
for line in open("aaa.txt"):
lst.append(line)
break

print lst
----------

Why doesn't break work in the first example?
1. Why are you breaking in the for loop? That will only give you the
first line.

2. Your issue is caused by buffered IO. Because of the buffered IO,
your code is equivalent to doing this:

lst = []
s = sys.stdin.read() #All* of the reading up to EOF is done upfront.

for line in s:
lst.append(line)
break # I'm sure you don't really want the break here though :)

One way to get around the buffering is to do this:

Expand|Select|Wrap|Line Numbers
  1. # Open a file or the input stream
  2. if filename:
  3. f = open(filename,"r")
  4. else:
  5. f = sys.stdin
  6.  
  7. # Then you check to see if your file is interactive
  8. if f.isatty():
  9. # This is unbuffered, and will iterate the same as f
  10. f = iter(raw_input(),"")
  11.  
  12. # Now do your stuff
  13. lst = []
  14. for line in f:
  15. lst.append(line)
  16.  
  17. print lst
  18.  
* well, not ALL, it will read in chunks. But, I think they are 4096
Byte chunks by default.

Apr 12 '07 #7
On Apr 12, 4:20 am, "7stud" <bbxx789_0...@yahoo.comwrote:
I can't break out of the for loop in this example:

------
import sys

lst = []
for line in sys.stdin:
lst.append(line)
break

print lst
-----------
You may want to look at a related issue:

http://www.python.org/sf/1633941

Thanks,
Raghu.
Apr 12 '07 #8
On Apr 12, 10:25 am, "Matimus" <mccre...@gmail.comwrote:
* well, not ALL, it will read in chunks. But, I think they are 4096
Byte chunks by default.
If you are referring to the read ahead buffer size, it is 8192 bytes.

Raghu.

Apr 12 '07 #9
Le jeudi 12 avril 2007 16:25, Matimus a écrit*:
# Then you check to see if your file is interactive
if f.isatty():
* * # This is unbuffered, and will iterate the same as f
* * f = iter(raw_input(),"")
This should be f = iter(raw_input,"") and this will end in a EOFError andstop
on blank line.
So you need a wrapper like :
>>def stdin_iterator() :
... while(True) :
... try : yield raw_input()
... except EOFError : return
...
>>f = stdin_iterator()
Do you really need to iterate on the file this way instead of using the
straightforward readline method ?
>>import sys
l=sys.stdin.readline()
>>while(l) :
... print l,
... l=sys.stdin.readline()


--
_____________

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
Apr 12 '07 #10
On Apr 12, 8:20 am, Maric Michaud <m...@aristote.infowrote:
Le jeudi 12 avril 2007 16:25, Matimus a écrit :
# Then you check to see if your file is interactive
if f.isatty():
# This is unbuffered, and will iterate the same as f
f = iter(raw_input(),"")

This should be f = iter(raw_input,"") and this will end in a EOFError and stop
on blank line.
So you need a wrapper like :
>def stdin_iterator() :

... while(True) :
... try : yield raw_input()
... except EOFError : return
...
>f = stdin_iterator()

Do you really need to iterate on the file this way instead of using the
straightforward readline method ?
>import sys
l=sys.stdin.readline()
while(l) :

... print l,
... l=sys.stdin.readline()

--
_____________

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
You are correct, I looked back to a program I wrote a while ago where
I ran into this issue. Here is the actual solution I used:

if f.isatty():
f = iter(f.readline,"")

Apr 13 '07 #11
Hi,

Thanks for the responses. My book, Beginning Python: From Novice to
Professional(p. 266) says that "sys.stdin is iterable, just like other
files", so I thought I would test it out. However, I don't see how
it is acting similar to a file in my example.

I assume all input is buffered by default, so I'm not sure how it
explains things to say that input from sys.stdin is buffered.
I typed many lines, but lst contains only one item, as expected. Same as
your regular file example: the file contains many lines, but only the
first goes into the list.
Interesting example--not as I expected! But there is a difference in
the two examples isn't there? When you iterate over a file, the whole
file isn't put into an internal buffer first, is it?

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.
I've wondered what the differences were. Thanks for bringing that
up. I searched around on google, and PEP 234 has this to say about
that:
------------------------------------
- Files implement a tp_iter slot that is equivalent to
iter(f.readline, ""). This means that we can write

for line in file:
...

as a shorthand for

for line in iter(file.readline, ""):
...

which is equivalent to, but faster than

while 1:
line = file.readline()
if not line:
break
...
....
Because the file iterator uses an internal buffer, mixing this
with other file operations (e.g. file.readline()) doesn't work
right. Also, the following code:

for line in file:
if line == "\n":
break
for line in file:
print line,

doesn't work as you might expect, because the iterator created by
the second for-loop doesn't take the buffer read-ahead by the
first for-loop into account. A correct way to write this is:

it = iter(file)
for line in it:
if line == "\n":
break
for line in
it:
print line,
--------------------------------------------

You may want to look at a related issue:
http://www.python.org/sf/1633941
Bad link.
>This should be f = iter(raw_input,"") and this will end in a EOFError
and stop on blank line. So you need a wrapper
Why a wrapper? This example seems to work without error:

lst = []

f = iter(raw_input, "")
for line in f:
lst.append(line)

print lst

Apr 13 '07 #12
7stud wrote:
I assume all input is buffered by default, so I'm not sure how it
explains things to say that input from sys.stdin is buffered.
The difference with sys.stdin is that it has indeterminate length until
you signal EOF. I believe you'd get the same problem reading from, say,
a named pipe.
>I typed many lines, but lst contains only one item, as expected. Same as
your regular file example: the file contains many lines, but only the
first goes into the list.

Interesting example--not as I expected! But there is a difference in
the two examples isn't there? When you iterate over a file, the whole
file isn't put into an internal buffer first, is it?
It is if the file is smaller than the buffer size.
>This should be f = iter(raw_input,"") and this will end in a EOFError
and stop on blank line. So you need a wrapper

Why a wrapper?
Because without a wrapper you'll get EOFError, while the file iterator
would ordinarily give you StopIteration.
--
Michael Hoffman
Apr 13 '07 #13
On Apr 13, 3:13 am, Michael Hoffman <cam.ac...@mh391.invalidwrote:
7stud wrote:
I assume all input is buffered by default, so I'm not sure how it
explains things to say that input from sys.stdin is buffered.

The difference with sys.stdin is that it has indeterminate length until
you signal EOF. I believe you'd get the same problem reading from, say,
a named pipe.
Couldn't you say the same thing about a file you are iterating over?

I typed many lines, but lst contains only one item, as expected. Same as
your regular file example: the file contains many lines, but only the
first goes into the list.
Interesting example--not as I expected! But there is a difference in
the two examples isn't there? When you iterate over a file, the whole
file isn't put into an internal buffer first, is it?

It is if the file is smaller than the buffer size.
How is that relevant?
This should be f = iter(raw_input,"") and this will end in a EOFError
and stop on blank line. So you need a wrapper
Why a wrapper?

Because without a wrapper you'll get EOFError, while the file iterator
would ordinarily give you StopIteration.
Did you run my example? Did you get an error? I don't get an error.

Apr 13 '07 #14
On Apr 13, 3:36 am, "7stud" <bbxx789_0...@yahoo.comwrote:
>
It is if the file is smaller than the buffer size.

How is that relevant?
If I put 100 lines of text in a file with each line having 50
characters, and I run this code:

import sys

lst = []
for line in open("aaa.txt"):
print "an iteration"
lst.append(line)
break

print lst
The output is:

$ python test1.py

an iteration
['helleo haljdfladj ahdflasdjf ds hdljfalsdjfdsljfds \n']

It seems clear to me that the whole file wasn't first read into a
buffer before the code started processing the data.

Apr 13 '07 #15
In <11**********************@p77g2000hsh.googlegroups .com>, 7stud wrote:
On Apr 13, 3:36 am, "7stud" <bbxx789_0...@yahoo.comwrote:
>>
It is if the file is smaller than the buffer size.

How is that relevant?

If I put 100 lines of text in a file with each line having 50
characters, and I run this code:

import sys

lst = []
for line in open("aaa.txt"):
print "an iteration"
lst.append(line)
break

print lst
The output is:

$ python test1.py

an iteration
['helleo haljdfladj ahdflasdjf ds hdljfalsdjfdsljfds \n']

It seems clear to me that the whole file wasn't first read into a
buffer before the code started processing the data.
How is that clear to you? Why do you think the file was not loaded
completely into a buffer and only the first line from this buffer ends up
in `lst`?

Let's try this:

def main():
test_file = open('test.txt', 'w')
for i in xrange(100):
test_file.write('%d %s\n' % (i, 'x' * 50))
test_file.close()

test_file = open('test.txt', 'r')
for line in test_file:
print line
break
print '>%s<' % test_file.read(75)
test_file.close()

Output:
0 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
><
You see the read after the loop doesn't read anything because the file
content is in the buffer of the iterator. If the test file has more
lines, I tested it with 1000, the output looks like this:

0 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>xx
151 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
152 xxxxxxxxxxxxx<

Ciao,
Marc 'BlackJack' Rintsch

Apr 13 '07 #16

On Apr 13, 2007, at 4:47 AM, 7stud wrote:
On Apr 13, 3:36 am, "7stud" <bbxx789_0...@yahoo.comwrote:
>>
>>It is if the file is smaller than the buffer size.

How is that relevant?

If I put 100 lines of text in a file with each line having 50
characters, and I run this code:

import sys

lst = []
for line in open("aaa.txt"):
print "an iteration"
lst.append(line)
break

print lst
The output is:

$ python test1.py

an iteration
['helleo haljdfladj ahdflasdjf ds hdljfalsdjfdsljfds \n']

It seems clear to me that the whole file wasn't first read into a
buffer before the code started processing the data.
The break statement causes it to bail after the first iteration, so
that doesn't really prove your point. For example:

lst = []
for line in ['we', 'all live', 'in a yellow', 'submarine']:
print "an iteration"
lst.append(line)
break

print lst

The output is:

an iteration
['we']
Apr 13 '07 #17
7stud wrote:
On Apr 13, 3:13 am, Michael Hoffman <cam.ac...@mh391.invalidwrote:
>7stud wrote:
>>I assume all input is buffered by default, so I'm not sure how it
explains things to say that input from sys.stdin is buffered.
The difference with sys.stdin is that it has indeterminate length until
you signal EOF. I believe you'd get the same problem reading from, say,
a named pipe.

Couldn't you say the same thing about a file you are iterating over?
Only if the file has indeterminate length. Regular files have a length.
>>>This should be f = iter(raw_input,"") and this will end in a EOFError
and stop on blank line. So you need a wrapper
Why a wrapper?
Because without a wrapper you'll get EOFError, while the file iterator
would ordinarily give you StopIteration.

Did you run my example? Did you get an error? I don't get an error.
Yes I did. I did get an error.
>>lst = []
f = iter(raw_input, "")
for line in f:
.... lst.append(line)
....
abc
def
<Ctrl-D>Traceback (most recent call last):
File "<stdin>", line 1, in <module>
EOFError
--
Michael Hoffman
Apr 13 '07 #18
On Apr 13, 6:20 am, Michael Hoffman <cam.ac...@mh391.invalidwrote:
7stud wrote:
On Apr 13, 3:13 am, Michael Hoffman <cam.ac...@mh391.invalidwrote:
7stud wrote:
I assume all input is buffered by default, so I'm not sure how it
explains things to say that input from sys.stdin is buffered.
The difference with sys.stdin is that it has indeterminate length until
you signal EOF. I believe you'd get the same problem reading from, say,
a named pipe.
Couldn't you say the same thing about a file you are iterating over?

Only if the file has indeterminate length. Regular files have a length.
>>This should be f = iter(raw_input,"") and this will end in a EOFError
and stop on blank line. So you need a wrapper
Why a wrapper?
Because without a wrapper you'll get EOFError, while the file iterator
would ordinarily give you StopIteration.
Did you run my example? Did you get an error? I don't get an error.

Yes I did. I did get an error.
>>lst = []
>>f = iter(raw_input, "")
>>for line in f:
... lst.append(line)
...
abc
def
<Ctrl-D>Traceback (most recent call last):
File "<stdin>", line 1, in <module>
EOFError
--
Michael Hoffman
But if you hit return on a blank line, there is no error. In other
words, will stop on a blank line and not return EOFError.

Anyway, it seems everyone is saying that when you iterate over a file,
the whole file is first read into memory. Therefore iterating over
sys.stdin is consistent: you have to type Ctrl+D to signal EOF before
the iteration can start. Is that about right?

Apr 14 '07 #19
In <11**********************@n59g2000hsh.googlegroups .com>, 7stud wrote:
Anyway, it seems everyone is saying that when you iterate over a file, the
whole file is first read into memory. Therefore iterating over sys.stdin
is consistent: you have to type Ctrl+D to signal EOF before the iteration
can start. Is that about right?
Not the whole file is read but a block of it. If you type in enough to
fill the buffer the iteration can start without an EOF on `sys.stdin`.

See the second example in <pa****************************@gmx.netwhich
demonstrates that not the whole file is read at once by the iterator.

Ciao,
Marc 'BlackJack' Rintsch
Apr 14 '07 #20
7stud wrote:
On Apr 13, 6:20 am, Michael Hoffman <cam.ac...@mh391.invalidwrote:
[...]
>
But if you hit return on a blank line, there is no error. In other
words, will stop on a blank line and not return EOFError.

Anyway, it seems everyone is saying that when you iterate over a file,
the whole file is first read into memory. Therefore iterating over
sys.stdin is consistent: you have to type Ctrl+D to signal EOF before
the iteration can start. Is that about right?
No. The file content is usually buffered, but the buffering doesn't
necessarily include the whole content of the file.

If you are iterating over the file the correct way to access the next
line is to call the file's .next() method, as I indicated before.

If you are reading lines the appropriate way is to use readline().

And, as you have already seen an error message telling you, mixing the
two types is unlikely to give you usable results.

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

Apr 14 '07 #21
On Apr 14, 7:43 am, Steve Holden <s...@holdenweb.comwrote:
7stud wrote:
On Apr 13, 6:20 am, Michael Hoffman <cam.ac...@mh391.invalidwrote:
[...]
But if you hit return on a blank line, there is no error. In other
words, will stop on a blank line and not return EOFError.
Anyway, it seems everyone is saying that when you iterate over a file,
the whole file is first read into memory. Therefore iterating over
sys.stdin is consistent: you have to type Ctrl+D to signal EOF before
the iteration can start. Is that about right?

No. The file content is usually buffered, but the buffering doesn't
necessarily include the whole content of the file.

If you are iterating over the file the correct way to access the next
line is to call the file's .next() method, as I indicated before.

If you are reading lines the appropriate way is to use readline().

And, as you have already seen an error message telling you, mixing the
two types is unlikely to give you usable results.

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
Does iterating over stdin work the same way? If one were to type in
enough lines to fill the buffer would iteration begin before entering
EOF with Ctrl+D?

Apr 14 '07 #22
7stud wrote:
On Apr 14, 7:43 am, Steve Holden <s...@holdenweb.comwrote:
>7stud wrote:
>>On Apr 13, 6:20 am, Michael Hoffman <cam.ac...@mh391.invalidwrote:
[...]
>>But if you hit return on a blank line, there is no error. In other
words, will stop on a blank line and not return EOFError.
Anyway, it seems everyone is saying that when you iterate over a file,
the whole file is first read into memory. Therefore iterating over
sys.stdin is consistent: you have to type Ctrl+D to signal EOF before
the iteration can start. Is that about right?
No. The file content is usually buffered, but the buffering doesn't
necessarily include the whole content of the file.

If you are iterating over the file the correct way to access the next
line is to call the file's .next() method, as I indicated before.

If you are reading lines the appropriate way is to use readline().

And, as you have already seen an error message telling you, mixing the
two types is unlikely to give you usable results.
Does iterating over stdin work the same way? If one were to type in
enough lines to fill the buffer would iteration begin before entering
EOF with Ctrl+D?
Why don't you try it and tell me? That's what the interactive
interpreter is for.

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

Apr 15 '07 #23
On Apr 15, 10:59 am, Steve Holden <s...@holdenweb.comwrote:
7stud wrote:
On Apr 14, 7:43 am, Steve Holden <s...@holdenweb.comwrote:
7stud wrote:
On Apr 13, 6:20 am, Michael Hoffman <cam.ac...@mh391.invalidwrote:
[...]
>But if you hit return on a blank line, there is no error. In other
words, will stop on a blank line and not return EOFError.
Anyway, it seems everyone is saying that when you iterate over a file,
the whole file is first read into memory. Therefore iterating over
sys.stdin is consistent: you have to type Ctrl+D to signal EOF before
the iteration can start. Is that about right?
No. The file content is usually buffered, but the buffering doesn't
necessarily include the whole content of the file.
If you are iterating over the file the correct way to access the next
line is to call the file's .next() method, as I indicated before.
If you are reading lines the appropriate way is to use readline().
And, as you have already seen an error message telling you, mixing the
two types is unlikely to give you usable results.
Does iterating over stdin work the same way? If one were to type in
enough lines to fill the buffer would iteration begin before entering
EOF with Ctrl+D?

Why don't you try it and tell me? That's what the interactive
interpreter is for.

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
I just typed in 700 lines of text, and the iteration hasn't begun
yet. Should I keep going?

Apr 15 '07 #24
En Sun, 15 Apr 2007 16:51:12 -0300, 7stud <bb**********@yahoo.com>
escribió:
I just typed in 700 lines of text, and the iteration hasn't begun
yet. Should I keep going?
How much text each line? Python uses an 8K buffer.

--
Gabriel Genellina
Apr 15 '07 #25
>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

I just typed in 700 lines of text, and the iteration hasn't begun
yet. Should I keep going?
Well, I'm not sure how much of a programmer you are if you don't get the
idea of writing a _program_ to do the typing....

-------- a.py -----------
import sys

counter = 0
try:
while True:
s = str(counter) + '\n'
counter += len(s)
sys.stdout.write(s)
except IOError:
sys.stderr.write(s)
------- b.py ------------

import sys

lst = []
for line in sys.stdin:
lst.append(line)
break

print lst

-----------------

Besides that, a little bit of logic thinking should make clear that
having to wait till EOF won't work too much for piping programs that
produce continously output with possibly terabytes of data. Even under
modern memory constraints...
Diez
Apr 15 '07 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
1
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is...
1
by: Lisa C | last post by:
Hi all. I am trying to set up a C++ program which will continuously read stdin from another applications stdout. I will continue reading until I read a message that will indicate to stop. Does...
6
by: chad kline | last post by:
FBSD 4.8/GCC //////// C-CODE: //////// char c; while ( 1 ) {
6
by: Charlie Zender | last post by:
Hi, I have a program which takes the output filename argument from stdin. Once the program knows the output filename, it tries to open it. If the output file exists, the program asks the user to...
8
by: orium69 | last post by:
Hi! how could i know if it was written something in stdin since the last time i read it? Tks...
3
by: joshuawilsonster | last post by:
All, I'm hoping you can provide some help. I have opened a temporary file, written to it, verified the correct amount was written, done freopen() for the file to stdin, then try read the...
3
by: Jon Harrop | last post by:
I can read bytes from a FileStream with stream.ReadByte() but that does not handle stdin. I can read from stdin with System.Console.In using reader.Read() but it is 6x slower. So how do I read...
5
by: Luis Zarrabeitia | last post by:
I have a problem with this piece of code: ==== import sys for line in sys.stdin: print "You said!", line ==== Namely, it seems that the stdin buffers the input, so there is no reply until ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.