Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

file

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?

like image 922
Ying Xiong Avatar asked Jul 05 '15 11:07

Ying Xiong


2 Answers

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)
like image 82
Mazdak Avatar answered Sep 29 '22 01:09

Mazdak


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.

like image 25
mikeb Avatar answered Sep 29 '22 01:09

mikeb