faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 5 people (60%) answered Yes
Recently 1 of 3 people (33%) answered Yes

Entry

while r.readline()... construction

Feb 14th, 2006 06:13
Scarfboy, Nathan Wallace, Hans Nowak, Snippet 28, Python Snippet Support Team


"""
Packages: text;files
"""

import sys

class Reader:
    def __init__(self, source):
        self.source = source
    def readline(self):
        line = self.source.readline()
        self.line = line
        return line # can be false if ""

r = Reader(sys.stdin)

while r.readline():
    sys.stdout.write("Another line: ")
    sys.stdout.write(r.line)



#Alternatively, file objects have been iterable since 2.3,
#which works as lazy readline()s, so the following is equivalent:
f = file('bigfile')
for line in f:
   sys.stdout.write(line)