6

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?

Kuba Spatny
  • 26,618
  • 9
  • 40
  • 63
Yuxiang Wang
  • 8,095
  • 13
  • 64
  • 95

4 Answers4

9

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.
Tadeck
  • 132,510
  • 28
  • 152
  • 198
4

I hate to split hairs, but the answer is either yes or no -- depending on exactly what you are asking.

Will the line, as written, close the file descriptor? Yes.

Will it happen automatically after this operation? Yes… but probably not immediately.

Will the file be closed immediately after this line? No, not likely (see reply above).

Python will delete the file descriptor when the reference count for the file object is zero. If you are opening the file in a local scope, such as inside a function (either as you have done, or even if you have assigned it to a local variable inside a def), when that local scope is cleaned up -- and if there are no remaining references to the file object, the file will be closed.

It is, however, a much better choice to be explicit, and open the file inside a with block -- which will close the file for you instead of waiting for the garbage collector to kick in.

You also have to be careful in the case where you unintentionally assign the file descriptor to a local variable… (see: opened file descriptors in python) but you aren't doing that. Also check the answers here (see: check what files are open in Python) for some ways that you can check what file descriptors are open, with different variants for whatever OS you are on.

Community
  • 1
  • 1
Mike McKerns
  • 33,715
  • 8
  • 119
  • 139
2

Yes, the return value of open has a lifetime only of that of the call to pickle.load. It closes the open file descriptor when closed.

If you're extremely paranoid, consider:

with open(fileName,'r') as fin:
    c = pickle.load(fin)

The with keyword establishes the lifetime of fin

Eric Urban
  • 3,671
  • 1
  • 18
  • 23
  • Are you sure about the file handler's lifetime? Could you post a link please? – inspectorG4dget Aug 01 '13 at 04:27
  • 1
    Python is garbage collected. The object gets cleaned up and closes its FD then. Just try it and look in /proc at the open file descriptors. – Eric Urban Aug 01 '13 at 13:27
  • Hi Eric, thank you for sharing! I really appreciate it. However, when I tried to look into the file opened in python, it is not closed... But I never knew how to check before seeing your post. It might be just my python distribution though... Thanks again! – Yuxiang Wang Aug 01 '13 at 17:14
  • You will need to execute several additional statements before the garbage collector gets around to closing it. At least from my experience. I am using Stackless 2.7.5 – Eric Urban Aug 01 '13 at 22:05
0

No it does not close the file. This is one place where you could use the with operator:

with open('path/to/file') as infile:
    c = pickle.load(f)

Further reading: this and this

Community
  • 1
  • 1
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241