Как использовать с открытым
with open('output.txt', 'w') as file: # Use file to refer to the file object
file.write('Hi there!')
# more down here
# https://www.pythonforbeginners.com/files/with-statement-in-python
KaptainKermit1