What are the consequences of not closing open files in Python? -
this question has answer here:
- is explicitly closing files important? 5 answers
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
Post a Comment