In python c = pickle.load(open(fileName, 'r')) does this close the file?

pickle, python

Solution

No, but you can simply adapt it to close the file:

# file not yet opened
with open(fileName, 'r') as f:
    # file opened
    c = pickle.load(f)
    # file opened
# file closed

What `with` statement does, is (among other things) calling `__exit__()` method of object listed in `with` statement (in this case: opened file), which in this case closes the file.

Regarding opened file's `__exit__()` method:

>>> f = open('deleteme.txt', 'w')
>>> help(f.__exit__)
Help on built-in function __exit__:

__exit__(...)
    __exit__(*excinfo) -> None.  Closes the file.

Problem

I tried to Google but cannot find an answer. If I just do ``` c = pickle.load(open(fileName, 'r')) ``` Will the file be automatically closed after this operation?

Original source

Related problems