Question

f=open('C:\\Python33\\text file.txt','r')

for c in iter(lambda: f.read(1),'\n'):
    print(?)

How would I print out the values which lambda: f.read(1) yields please?

Was it helpful?

Solution

Just print the c. That's what you're getting from iter() in your for loop.

f=open('C:\\Python33\\text file.txt','r')

for c in iter(lambda: f.read(1),'\n'):
    print(c)

Small improvement suggestion, use the with-statement:

with open('C:\\Python33\\text file.txt', 'r') as f:
    for c in iter(lambda: f.read(1), '\n'):
        print(c)

This way you won't have to call f.close().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top