reading a file line by line using a generator

Solutions on MaxInterview for reading a file line by line using a generator by the best coders in the world

showing results for - "reading a file line by line using a generator"
Ben
24 Jan 2018
1def read_file(path, block_size=1024): 
2    with open(path, 'rb') as f: 
3        while True: 
4            piece = f.read(block_size) 
5            if piece: 
6                yield piece 
7            else: 
8                return
9
10for piece in read_file(path):
11    process_piece(piece)
12