The solution to the following link did not solve my issue: How do I close a file object I never assigned to a variable?
How do I close the file in the following line?
file = open(filename, 'r').read().splitlines()
The solution to the following link did not solve my issue: How do I close a file object I never assigned to a variable?
How do I close the file in the following line?
file = open(filename, 'r').read().splitlines()
Such a file is automatically garbage collected and closed. However, this pattern should generally be avoided. Instead, use the with
statement:
with open(filename, 'r') as fd:
lines = fd.read().splitlines()