generate_packets.py: Use context managers and with-blocks when accessing files
Part of #43927. Context managers also allow things that need to happen at the beginning and end of something – like multiple inclusion protection or extern "C" {...} wrapping – to have both parts in one place.
The part about more context managers has been split off into #43953 and #43954.
Part of #43927. Context managers also allow things that need to happen at the beginning and end of something – like multiple inclusion protection or extern "C" {...} wrapping – to have both parts in one place.