That you read the whole file at once has no impact on when the file is closed. No matter what you do with the file (short of explicitly closing it), without with
the file remains open until its __del__
method is called. So the reason for using with
apply just as much to your use case as to every other case of file I/O:
It definitely closes the file as early as possible. It's immune to reference cycles and it's more portable to other Python implementations (imagine the anger when you could get a 5x performance increase from PyPy but can't use it because your program crashes from having too many files open!). Even if it doesn't matter for the current context, code gets moved around and requirements change. It's an easy way (just one extra line!) to future proof code.
If it's a one-off script that will only open two files per run, it's still good style and a good habit. Same reason you don't change indentation to five spaces in one-off scripts.