In Python, if I return inside a “with” block, will the file still close?

Consider the following:

with open(path, mode) as f:
    return [line for line in f if condition]

Will the file be closed properly, or does using return somehow bypass the context manager?

4 Answers
4

Leave a Comment