CodeQL queries 1.24
If a file is opened then it should always be closed again, even if an exception is raised. Failing to ensure that all files are closed may result in failure due to too many open files.
Ensure that if you open a file it is always closed on exiting the method.
Wrap the code between the
functions in a
with statement or use a
statement. Using a
with statement is preferred as it is shorter
and more readable.
The following code shows examples of different ways of closing a file. In the first example, the file is closed only if the method is exited successfully. In the other examples, the file is always closed on exiting the method.