+ 2

Need for the close function

Why is it necessary to use the close function. Doesn't Python automatically close the file after the program is run?

11th Aug 2016, 6:29 AM
Aditya Shukla
Aditya Shukla - avatar
5 Answers
+ 1
Nope python doesn't do it for you, you can make it automatic with a try finally statement: try: #openfile, do something finally: #no matter what happens file.close()
11th Aug 2016, 1:11 PM
Marco Romanin
Marco Romanin - avatar
+ 1
You can have statement like --> with open(filename): This will automatically close your file
16th Aug 2016, 7:00 AM
Viswanathan
Viswanathan - avatar
+ 1
@Viswanathan , can you explain it Better? i didn't understand
16th Aug 2016, 8:37 AM
Marco Romanin
Marco Romanin - avatar
0
you got to close it to make the file to be updated when you write it... it will be saved with the new text (when you are in writing mode)
29th Aug 2016, 7:38 AM
Giovanni Gatto
Giovanni Gatto - avatar
0
the .close() is saving the changes you've done to the file
11th Jan 2017, 9:31 PM
Alex
Alex - avatar