TFBlogs_2017 icon indicating copy to clipboard operation
TFBlogs_2017 copied to clipboard

file close in with scope

Open BetterYan opened this issue 7 years ago • 1 comments

    with open(file, "wb") as f:
        f.write(data)
        f.close()

Why use f.close() in with scope? To my understanding, 'with' syntax wll do release task by itself. Am I right?

BetterYan avatar Dec 19 '17 06:12 BetterYan

Yes with will close the file, so f.close() is not required.

see this to know how deep this rabbit hole goes - http://blog.lerner.co.il/dont-use-python-close-files-answer-depends/

electron0zero avatar Feb 22 '18 19:02 electron0zero