Why this matters
Prevents crashes when files are missing, locked, or malformed.
Guard file IO with try/except; handle OSError and data parse errors with clear messages.
Prevents crashes when files are missing, locked, or malformed.
Side-by-side examples engineers can pattern-match during review.
data = open(path).read()import json
try:
with open(path, 'r', encoding='utf-8') as f:
data = json.load(f)
except FileNotFoundError:
logger.error('file not found', extra={'path': path})
except (OSError, json.JSONDecodeError) as e:
logger.exception('read failed', extra={'path': path})
raiseopen(path).read()with open(path) as f: ...; # try/except aroundFrom the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.