How do I close a file in python after opening it this way:
line = open("file.txt", "r").readlines()[7]
Best to use a context manager. This will close the file automatically at the end of the block and doesn't rely on implementation details of the garbarge collection
with open("file.txt", "r") as f:
line = f.readlines()[7]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With