0

Usually it is required to call the .close() method on a file object or use the "with open" construct to make sure it gets closed.

However, I am wondering if writing to a file like this leaves the file closed:

open(path,'w').writelines(fileContents)
Tomas Ruiz
  • 139
  • 4
  • 15

1 Answers1

5

No, open(path,'w').writelines(fileContents) does not close the file. It's left in an open state, even though the file object is anonymous and it's not assigned to anything. CPython will clean up the handle if it goes out of scope.

Furthermore, I believe garbage collection would have to occur, which may or may not happen at the end of a block (it usually does, but there's no mandate or guarantee AFAIK). Other Python interpreters may or may not close the file when it goes out of scope, so don't rely on that mechanism for general Python code.

It's best to just use the with idiom and be done with it. There are very few reasons not to use with when dealing with file descriptors.

Matt Messersmith
  • 12,939
  • 6
  • 51
  • 52
  • Does CPython really clean up the handle if it goes out of scope or only when the GC runs the next time? – syntonym Oct 03 '18 at 12:17
  • @syntonym That depends. If the file object's refcount drops to 0, it's immediately cleaned up. If there's some kind of reference cycle keeping it alive, it'll only be disposed when the garbage collector takes care of it. – Aran-Fey Oct 03 '18 at 12:18
  • @syntonym I think it's only when GC runs. I believe `__del__` must be called. Either way, it's not *super* relevant imo (although it is interesting). I think always using `with` is the way to go. Relying on GC or specific interpreters to clean up file handles is risky business – Matt Messersmith Oct 03 '18 at 12:19
  • @MattMessersmith If the object is removed as soon as the refcount drops to 0 it's actually kinda okay. But I also think the file only gets closed in the next GC cycle but then really weird things can happen like running out of file handles although you only open one file at a time. Thus I think the argument for using `with` is even stronger, it's not only about other implementations (why should I care about other versions when I only use CPython?) but the weird things are already happening if you write an "innocent looking" program in standard CPython. – syntonym Oct 03 '18 at 13:55