© 2026 LinearBytes Inc.
Search posts, tags, users, and pages
Mark
The better way is
with open("file name", "r") as fh: content = fh.read()
Then it's automatically closed after the scope ends.
As a bonus tip, .read().splitlines() is better than .readlines() because it also stripts the newline characters.
.read().splitlines()
.readlines()
Yeah, what Mark said. Also, that seems like a really easy one to Google.
Joseph S Stevens Yeah but there's also a lot of wrong info out there. That link in the opening post that suggests the wrong old approach is from just a few days ago... Guess we're more reliable than Google :-)