1def count_errors(log_path):
2 f = open(log_path)
3 lines = f.readlines()
4 error_count = sum(1 for l in lines if 'ERROR' in l)
5 f.close()
6 return error_count
7
8# If an exception occurs between open() and close(),
9# the file handle leaks until garbage collection
no lines flagged
#045PracticeEasy8 min · 50 XP
Context Manager Not Used for File Handle
A log parser leaks file handles under error conditions because files are opened without a context manager.
Flagged linesNo lines flagged yet
What's wrong?
Flag a line or write a note to submit.