-1

In python, if i opened a file:

file = open('file.txt','r')
//  read the file and stuff
file = None // does this close the file and free up resources?

then set it to something else, would that file be closed? would there be any downsides to doing this?

I am aware that 'file.close()' does this, but i want to know if i can just set it to something else instead.

a1cd
  • 17,884
  • 4
  • 8
  • 28
  • 2
    `file.close()` does. You can open file with `with` it closes the file for you. – Ch3steR Nov 13 '21 at 17:25
  • Yep, i know, i am wondering if this will too – a1cd Nov 13 '21 at 17:26
  • 2
    No, it will not. – Thomas Hilger Nov 13 '21 at 17:27
  • 2
    Why would it close the file if you assign it `None`? Variable names are just references to objects in memory. `file = None` means the name `file` now refers to `None` your file object hasn't closed. – Ch3steR Nov 13 '21 at 17:27
  • Strictly speaking I think your file object now still floats somewhere in memory until the garbage collector eats it...but the latter will not call `close` ... or does it? – Thomas Hilger Nov 13 '21 at 17:29
  • https://stackoverflow.com/a/36047108/7084566 – Thomas Hilger Nov 13 '21 at 17:34
  • @ThomasHilger Thanks! So i could do that, and garbage collection would eventually get it, its bad practice, i would confuse anyone who looked at the code, but... i could – a1cd Nov 13 '21 at 17:37
  • @ThomasHilger How does stackoverflow.com/a/36047108/7084566 answer my question? – a1cd Nov 13 '21 at 17:38
  • 3
    To be clear, this works perfectly with the C implementation of python. The file object reference count goes to zero and the underlying file is flushed and closed during the `file = None` assignment. If you have more references to the file, it doesn't close until the last one goes away. And if you have circular references, it may not close until the garbage collector runs, or maybe not at all. Some other implementations of python only use garbage collection so the file doesn't close during `file =None`. – tdelaney Nov 13 '21 at 17:58
  • @Evergreen https://stackoverflow.com/a/36047108/7084566 explains in some detail what @tdelaney also explained. The assignment `file = None` does not close anything. It just creates a`Nonetype` and assignes its reference to a variable named `file` (eventually the same variable name, but this new assigment does not know this). The previous relation of the variable `file` to the file-object (lets say somewhere in memory) gets lost without the file-object knowing about this pittyful loss. If the file-object is closed or not depends on other details (e.g. actually used interpreter implementation). – Thomas Hilger Nov 14 '21 at 11:46
  • 2
    @ThomasHilger - Minor clarification - the file object knows about the loss because its reference count is reduced by 1. If cpython sees that the reference count is zero, it calls the object's `__del__`. A file object flushes and closes the file on `__del__`. Circular refernces can keep the reference count from going to zero. And if the python implementation always frees objects using a garbage collector, the close would happen at some indeterminate time in the future. – tdelaney Nov 14 '21 at 18:18
  • @tdelaney THX! It's never too late to learn more details. :-) – Thomas Hilger Nov 15 '21 at 13:04

1 Answers1

1

You should always close the file every time you open it unless you are using with open.