Bug Report
Description
When an exception is raised during a file operation, the file handle may not be properly closed, leading to a resource leak.
Steps to Reproduce
- Open a file without using a context manager
- Raise an exception before closing the file
- The file handle remains open
Expected Behavior
File handles should always be closed, even when exceptions occur.
Suggested Fix
Use a with statement (context manager) to ensure the file is always closed:
# Instead of:
f = open('file.txt', 'r')
data = f.read()
f.close()
# Use:
with open('file.txt', 'r') as f:
data = f.read()
Environment
This is a common Python pitfall that can cause issues in long-running applications.
Bug Report
Description
When an exception is raised during a file operation, the file handle may not be properly closed, leading to a resource leak.
Steps to Reproduce
Expected Behavior
File handles should always be closed, even when exceptions occur.
Suggested Fix
Use a
withstatement (context manager) to ensure the file is always closed:Environment
This is a common Python pitfall that can cause issues in long-running applications.