reading a file line by line using a generator 1

reading a file line by line using a generator

def read_file(path, block_size=1024): 
    with open(path, 'rb') as f: 
        while True: 
            piece = f.read(block_size) 
            if piece: 
                yield piece 
            else: 
                return

for piece in read_file(path):
    process_piece(piece)

Here is what the above code is Doing:
1. The read_file function is a generator. It yields a piece of the file at a time.
2. The for loop calls read_file, which returns a generator object.
3. The for loop calls the generator object’s __next__ method to get the next piece of the file.
4. The generator yields the piece of the file.
5. The for loop calls process_piece with the piece of the file.
6. The for loop calls the generator object’s __next__ method to get the next piece of the file.
7. The generator yields the piece of the file.
8. The for loop calls process_piece with the piece of the file.
9. The for loop calls the generator object’s __next__ method to get the next piece of the file.
10. The generator returns, which causes the for loop to terminate.

Similar Posts