open in function
This is not the recommended way to open a file, but this is how it was done before the introduction of the with context manager.
Here we have the same issue. We have a conditional call to return where we forgot to close the file.
examples/advanced/no_context_fh.py
import sys import re def do_something(filename): fh = open(filename) while True: line = fh.readline() if line is None: break line = line.rstrip("\n") if re.search(r'\A\s*\Z', line): return print(line) fh.close() def main(): if len(sys.argv) != 2: exit(f"Usage: {sys.argv[0]} FILENAME") filename = sys.argv[1] do_something(filename) main()