7

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?

1
  • 1
    Yeah, the file object will be closed once it is garbage collected. It doesn't matter whether cPickle throws exception or not. But since we don't have control over when will that happen it really is a bad practice, i.e. stick with with. Commented Jul 5, 2015 at 11:33

2 Answers 2

4

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.