9.9 finally
Clause
Operating systems typically can prevent more than one program from manipulating a file at once. When a program finishes processing a file, the program should close it to release the resource. This enables other programs to use the file (if they’re allowed to access it). Closing the file helps prevent a resource leak in which the file resource is not available to other programs because a program using the file never closes it.
The finally
Clause of the try
Statement
A try
statement may have a finally
clause as its last clause after any except
clauses or else
clause. The finally
clause is guaranteed to execute, regardless of whether its try
suite executes successfully or an exception occurs.8 In other languages that have ...
Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.