MC logo
Copy Input to Output I
[^] Code Examples
<<Break and Continue copy1.py Copy Input to Output II>>

# Script to copy standard input to standard output, one line at a time.

# This gets various items to interfaces with the OS, including the
# standard input stream.
import sys

# Readline is a method of stdin, which is in the standard object sys.
# It returns the empty string on EOF.
line = sys.stdin.readline()

# The string line works as the while test.  As several other scripting
# languages, the empty string is treated as false, other strings are treated
# as true.
while line:
    # Print the line read.  Since readline leaves the terminating newline,
    # a slice is used to print all characters in the string but the last.
    # Otherwise, each input line would be output with two line terminators.

    # Next line.
    line = sys.stdin.readline()

Since Python has assignment statements instead of assignment expressions, one cannot write:

while line = sys.stdin.readline():

Which would be more concise.

It's also worth noting that readline's habit of leaving the newline on the end is essential to making this work. Otherwise, an empty line in the input file would be indistinguishable from EOF.
<<Break and Continue Copy Input to Output II>>