SJPRO
SJPRO

Reputation: 313

Is file closing necessary in this situation?

if I have:

fdata = open(pathf, "r").read().splitlines()

Will the file automatically close after getting the data? If not how can I close it since fdata is not a handle?

Thank you

Upvotes: 4

Views: 193

Answers (4)

heemayl
heemayl

Reputation: 42127

Will the file automatically close after getting the data?

In your example, fdata is actually a list, not a file object. The file object is what returned by open().

If you had a name bound to a file object or fdata were a file object, the answer would be it depends.

If the file object, does not have any reference present i.e. it's reference count reaches 0, it will be garbage collected and hence will be destroyed in the process.

If not how can I close it since fdata is not a handle?

You can't as fdata is not a file object (like you mentioned) and you don't have any reference to the file object returned by open() either.

If you had a file object, you could explicitly call close() on it:

f_object.close()

Better yet, as the open is a context manager, use the with ... construct to let it close automatically upon the block end:

with open('file.txt') as f_object:
    ...

One added advantage is that the file will be closed in case of an exception too. If you are interested, check the __enter__ and __exit__ special methods of open.

Upvotes: 3

sacuL
sacuL

Reputation: 51425

If you use this:

with open(pathf, 'r') as f:
     fdata = f.read().splitlines()

Then you don't have to close your file, it is done automatically. It's always good practice to have your files closed after you are done using them (reduces the risk of memory leaks, etc...)

Upvotes: 3

T.S.
T.S.

Reputation: 302

The file will be automatically closed during exit or garbage collection. But as best practices matter, the better approach would be to use a context manager such as below:

with open(pathf, "r") as f:
    fdata = f.read().splitlines()

Thank you.

Upvotes: 3

Patrick Artner
Patrick Artner

Reputation: 51683

Use

with open(pathf, "r") as r:
    fdata = r.read().splitlines()
# as soon as you leave the with-scope, the file is autoclosed, even if exceptions happen.

Its not only about auto-closing, but also about correct closing in case of exceptions.

Doku: methods of file objects

It is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point. Using with is also much shorter than writing equivalent try-finally blocks:

If you’re not using the with keyword, then you should call f.close() to close the file and immediately free up any system resources used by it.
If you don’t explicitly close a file, Python’s garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while. Another risk is that different Python implementations will do this clean-up at different times.

Upvotes: 8

Related Questions