Ying Xiong
Ying Xiong

Reputation: 4948

When it is necessary to close a file and when it is not in python?

I was trying to write code that manages resources in a responsible way. I understand that a common idiom to make sure a file is closed after using is

with open("filename.txt", 'r') as f:
    # ...use file `f`...

However, I often see people do the following:

data = cPickle.load(open("filename.pkl", 'r'))

I am wondering is that a right way and does Python always close filename.pkl even if cPickle throws an exception? Some explanation (or pointers to articles that explains) whether that is safe?

Upvotes: 7

Views: 2186

Answers (2)

Kasravnd
Kasravnd

Reputation: 107347

Python does not close the file for you automatically because it doesn't know when you're done with the file object. You need to either close the file explicitly or wrap your code (which contains the open(...) function) in a with statement. Here is an example form python documentation about pickle module :

import pprint, pickle

pkl_file = open('data.pkl', 'rb')

data1 = pickle.load(pkl_file)
pprint.pprint(data1)

data2 = pickle.load(pkl_file)
pprint.pprint(data2)

pkl_file.close()

And using with which is a much more Pythonic approach, you can do:

with open("filename.pkl", 'r') as f:
    data = cPickle.load(f)

Upvotes: 4

mikeb
mikeb

Reputation: 11307

You can see the documentation here

If you use something like:

f = open('filename', 'r')
...
# Always close
f.close()

Or, like this:

with open('workfile', 'r') as f:
    read_data = f.read()
f.closed # True

You can also use finally, which is made for resource cleanup:

try:
     f = open('filename', 'r')
finally:
     if f and not f.closed:
          f.close()

In your example, the file should be closed, even though the GC might handle it, it's better to do it explicitly.

Upvotes: 1

Related Questions