0

In Python, it is recommended to use with when opening files, e.g.:

with open('x.json') as f:
  data = json.load(f)

But is it okay to shorten it as follows?

data = json.load(open('x.json'))

The with statement makes sure that the file is closed, but would Python close the file in my second example as well, as there's no pointer to the file and it's clear that the file won't be accessed anymore?

Markus
  • 1,635
  • 15
  • 17
  • 1
    Does this answer your question? [Python opening and reading files one liner](https://stackoverflow.com/questions/28680636/python-opening-and-reading-files-one-liner) (specifically, the 1st part of the accepted answer should address whether not using `with` is "okay") – Gino Mempin May 07 '23 at 23:52

1 Answers1

2

In the second example, the file is still open in read mode. It is essentially equivalent to:

f = open('x.json')
data = json.load(f)
f = None

# f may still be open!

To avoid possible issues with open file handles, one way is to close it afterwards:

f = open('x.json')
data = json.load(f)
f.close()

However, in some cases, close() may not always be called, which may or may not be problematic. To ensure it is, we must wrap it with a try, finally, or easier, with with:

with open('x.json') as f:
    data = json.load(f)
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
  • 1
    ...That said, if and only if you're hacking in a REPL (interactive interpreter), it's not the end of the world to be lazy and just write `data = json.load(open('x.json'))`. – Mateen Ulhaq May 07 '23 at 23:55
  • 1
    The difference between OP's code and your example is that you still have a reference to the file object (`f`), whereas OP created it anonymously so that no references to it outlive the `json.load` call. I'm not sure how prompt the GC necessarily is about destroying the file object (thereby implicitly closing the file handle, I think) but the two examples are not exactly equivalent. – Samwise May 08 '23 at 01:04
  • With the edit your code behaves more like OP's, but it's not really clear why not closing the file explicitly is a bad thing. Maybe it would help to demonstrate the possible issues you're talking about (like with subsequent code that opens the file for write, maybe). I played around a little bit with trying to get something bad/surprising to happen and couldn't. – Samwise May 08 '23 at 01:32
  • @Samwise I'm not an expert on this topic, but I think such issues are admittedly rare (e.g. a "Too many open files" error). Arguably, multiple processes doing I/O on the same file has its own problems (e.g. race conditions, mutability) that aren't directly solved by diligently closing the file handles. – Mateen Ulhaq May 08 '23 at 01:37
  • Open file handles will be closed regardless once the process exits, so if your script is only opening one specific (hard-coded) file, or even a larger number that's still below the handle limit, running out of handles isn't an issue even if you have a lot of dangling references in your script that prevent them from being closed automatically within the lifetime of the script. I think actual logic problems around not closing a file are more likely to pop up with writes than reads. – Samwise May 08 '23 at 01:47