What are the consequences of not closing open files in Python? -


this question has answer here:

i've seen best practice working files in python use with block:

with open('file', 'r') fi:  text = fi.read()  open('file', 'w') fi:   fi.write(text) 

this way files automatically closed after you're done them. lazy, , in quick one-shot scripts tend instead:

text = open('file', 'r').read() open('file', 'w').write(text) 

now if i'm writing real software™ should use former, i'd know consequences latter has (if any)?

on cpython: none; files closed when reference count drops 0, when .read() , .write() calls return.

on other python implementations not use reference counting, file remain open until garbage collected.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -