create an iterator that print each character in a file
until it returns '\n'.
#create an iterator that calls f.read(1) until it returns '\n'.
#syntax: iter(callable, sentinel) -> iterator;
the callable is called until it returns the sentinel
def next_line(f):
for c in iter(lambda: f.read(1),'\n'):
print "------->", c
fp = open('test_file', 'r')
next_line(fp)
#Suppose file 'test_file' contain line 'hello'
Output
=====
------->h
------->e
------->l
------->l
------->o
No comments:
Post a Comment