with
statements can be merged PTC-W0062With
statements`58
59
60def copy_file(file1, file2):
61 with open(file1, "rb") as f1:62 with open(file2, "wb") as f2:
63 f2.write(f1.read())
Nested with
statements can be merged into a single compound with
statement.
This issue is raised only when there are no other statements between the nested with
statements.
Merging collapsible with
statements can decrease cognitive complexity, improving code readability.
with open("file1", "w") as file1:
with open("file2", "w") as file2:
doSomething()
with open("file1", "w") as file1, open("file2", "w") as file2:
doSomething()