2

Is there a need to close a file after doing:

lines = open(fname).readlines()

Or does readlines() close the file after reading the data? If not, how should it be closed?

FI-Info
  • 635
  • 7
  • 16
  • 1
    `with open(fname) as file: ..` - https://effbot.org/zone/python-with-statement.htm (eg.) – user2864740 Oct 18 '19 at 22:13
  • FWIW, the question might be slightly more interesting if it was: "Does `open(file).readlines()` automatically close the file?"; it's also a slightly different question, as readlines is merely using the open file in such a case, and not presented as being explicitly in control (which it is not). – user2864740 Oct 18 '19 at 22:18
  • 1
    The reason why this distinction matters: `global_f = open(file); global_f.readlines()` - the file will not be closed until the program ends. – user2864740 Oct 18 '19 at 22:24
  • 1
    Possible duplicate of [Does fp.readlines() close a file?](https://stackoverflow.com/questions/16508271/does-fp-readlines-close-a-file) – Georgy Oct 24 '19 at 14:38

4 Answers4

6

It's easy enough to check empirically whether readlines closes the file:

>>> f = open("so.py")
>>> lines = f.readlines()
>>> f.closed
False
>>> f.close()
>>> f.closed
True

A little thought suggests that readlines should not close the file: even with the file "bookmark" at EOF, there are useful commands, starting with seek.

Prune
  • 76,765
  • 14
  • 60
  • 81
3

No, the method itself does not close the file automatically. At some point (reliably?) it will be closed if there are no more references to it anywhere in your code, but that is not done by the readlines method. You can either do the closing explicitly:

f = open(...)
lines = f.readlines()
f.close()

Or:

lines = []
with open(...) as f:
    lines = f.readlines()

Or depend on the garbage collector to do it for you, by not maintaining any reference to the file object:

lines = open(...).readlines()

Which is what you have already, and which will probably be fine in most circumstances. I don't know the level of guarantee that the garbage collector gives you there.

mypetlion
  • 2,415
  • 5
  • 18
  • 22
  • Won't the 'GC' ensure `file.close` "at some point in the very near future" in this case? There is no cylic ref established AFAIK and CPython has a relatively deterministic implementation.. whether "correct" or not, it's a different pattern than holding onto the opened file in a variable (eg.) – user2864740 Oct 18 '19 at 22:15
  • 2
    Isn't `lines` a list of strings? It won't have a `close` method. – Mark Ransom Oct 18 '19 at 22:16
  • In the presented example in the question, the file object is not saved in any visible/leaked intermediary name.. – user2864740 Oct 18 '19 at 22:23
  • @user2864740 Yes, but the question is whether the `readlines` method does the actual closing. Which it doesn't, so I was trying to keep my explanation more general in case OP had other use cases in mind. – mypetlion Oct 18 '19 at 22:24
3

You could use a with statement if you really wanted to, which would close the file without you having to call f.close(). (See here for guidance on using with in Python.) But @mypetition's answer is certainly the least painful option.

Tom Hosker
  • 526
  • 2
  • 17
1

It doesn't close the file, but you don't have to worry about it, Your file will be closed automatically before it's Garbage collected.

CPython use reference count to clear objects and clearly there is no variable pointing to the object returned by open, so it will be Garbage collected, and python close them before that.

Charif DZ
  • 14,415
  • 3
  • 21
  • 40