If you read an entire file with
content = open('Path/to/file', 'r').read() is the file handle left open until the script exits? Is there a more concise method to read a whole file?
The answer to that question depends somewhat on the particular Python implementation.
To understand what this is all about, pay particular attention to the actual
file object. In your code, that object is mentioned only once, in an expression, and becomes inaccessible immediately after the
read() call returns.
This means that the file object is garbage. The only remaining question is "When will the garbage collector collect the file object?".
in CPython, which uses a reference counter, this kind of garbage is noticed immediately, and so it will be collected immediately. This is not generally true of other python implementations.
A better solution, to make sure that the file is closed, is this pattern:
with open('Path/to/file', 'r') as content_file: content = content_file.read()
which will always close the file immediately after the block ends; even if an exception occurs.
Edit: To put a finer point on it:
file.__exit__(), which is "automatically" called in a
with context manager setting, the only other way that
file.close() is automatically called (that is, other than explicitly calling it yourself,) is via
file.__del__(). This leads us to the question of when does
__del__() get called?
A correctly-written program cannot assume that finalizers will ever run at any point prior to program termination.
Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether — it is a matter of implementation quality how garbage collection is implemented, as long as no objects are collected that are still reachable.
CPython currently uses a reference-counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references.
but as it suggests, other implementations may have other behavior. As an example, PyPy has 6 different garbage collection implementations!
You can use pathlib.
For Python 3.5 and above:
from pathlib import Path contents = Path(file_path).read_text()
For lower versions of Python use pathlib2:
$ pip install pathlib2
from pathlib2 import Path contents = Path(file_path).read_text()
This is the actual
def read_text(self, encoding=None, errors=None): """ Open the file in text mode, read it, and close the file. """ with self.open(mode='r', encoding=encoding, errors=errors) as f: return f.read()