Jul-03-2022, 05:45 AM
(Jun-30-2022, 01:21 PM)Mark17 Wrote:(Jun-29-2022, 01:42 PM)Oshadha Wrote: Without using filename = open(filename, 'w')
Try to use With:
with open(filename, 'w') as file: file.write('text')This way you don't have tofilename.close()
. This way is more efficient and automatically closes the file if an error is raised.
Does the entire program need to go inside this with block, then, or would I just include this in those places I'm actually writing to the file?
All code that requires the file text wrapper (
file
variable).Basically only when writing / reading the file.