[Tutor] newbie question [standard input]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Tue Apr 8 17:35:08 2003

On Tue, 8 Apr 2003 tpc@csua.berkeley.edu wrote:

> Hello Danny, thank you for letting me know about the tutor list.

Hello!  Welcome aboard!

> I have joined and I thought I should direct this question to you since
> you wrote the manual I am learning Python from.

Uh oh.  The quicky introduction I wrote at:


is really a brief dive into Python.  For more comprehensive and complete
Python tutorials, you may want to look at:


instead.  Please understand that I wrote that introduction.ps to help
advertise Python, but that introduction was never meant to survive beyond
that semester...  *grin*

> On pages 12 and 13 you specify two programs to take words from standard
> input

[some text cut]

> The problem is when I try to run these programs standalone it seems to
> hang and I have to hit Ctrl-d which gives me this message:
> Traceback (most recent call last):
>   File "/usr/local/apache2/htdocs/test.py", line 7, in ?
>     w = strip(sys.stdin.readline())
> KeyboardInterrupt
> When I try to run the program with arguments from the command line it
> hangs and I have to hit Ctrl-d which returns nothing:
> [root@nike frsbapp]# /usr/local/apache2/htdocs/test.py good good morning
> [root@nike frsbapp]# /usr/local/apache2/htdocs/test.py good good morning
> [root@nike frsbapp]# /usr/local/apache2/htdocs/test.py good good morning

Ah!  The term 'standard input' is subtly different from 'command line
argument'; although they are both conceptually "inputs" to a program, they
are two different entry points and are treated separately.  This can be
initially confusing at first --- and now that you bring it up, it does
seem slightly kludgy... hmmm...

Anyway, the statements above have sent to the 'test.py' the three command
line arguments: ['good', 'good', 'morning'].  Our dilemma is that the
program is looking for content within the 'standard input' passage, and
not through the command line arguments.

Let's see how to feed data through the 'standard input' method.  Try
preparing a text file called 'sample.txt', and then try from your shell:

    $ cat sample.txt | python test.py

The command above is an example of a "pipeline": the pipe symbol '|' tells
the system to take the output of the first command, and feed it as
standard input to the second command.

The unix command 'cat' takes all filenames in its command line arguments,
opens each file, and spews the contents into standard output... and the
pipeline turns the standard output of the left hand side into the standard
input of the right hand side.

Play around with pipelines a bit, and the concept of standard input and
output should become clearer.

If we go back to what had been happening before: What was happening
originally was that, because the program had not been started off from a
pipeline, your Unix operating system was assuming that the 'standard
input' would come from stuff that you type in next.  The program was not
actually hanging, but just waiting for you to start typing.  *grin* So if
you want, try:

    python test.py

and then start typing lines of words, one word per line.  Finally, press
Control-D to tell the system that you're done typing into standard input.

[Slightly off topic:

It looks like you're also beginning to learn Unix on your own system!
Cool.  Since it's the beginning of the spring semester in Berkeley, I
think the Self Paced Center (SPC) is open: there are some Unix tutors
there that you can talk to:


The folks at the SPC are pretty friendly; if you see Carol, say hi for me.

You should definitely NOT normally be running programs as the 'root' user
on your machine.  I recommend adding and using a regular non-privileged
user account while you experiment with Python.]

Good luck to you!