ajax_velu
ajax_velu

Reputation: 296

Does python close the file after it comes out?

I tired two code examples , one with close and the other with out close, strace says both code has called .close()

is it safe to understand python3.6 does an automatic close:

Eg1: the lines in the file is :

f = open('./myConfig.cfg')
jsonda = json.loads(f.read())

The corresponding strace o/p is

open("./myConfig.cfg", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=693, ...}) = 0
ioctl(3, TCGETS, 0x7ffe829a80a0)        = -1 ENOTTY (Inappropriate ioctl for device)
lseek(3, 0, SEEK_CUR)                   = 0
ioctl(3, TCGETS, 0x7ffe829a8040)        = -1 ENOTTY (Inappropriate ioctl for device)
lseek(3, 0, SEEK_CUR)                   = 0
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=693, ...}) = 0
read(3, "{\n  \"osns\" : [\n    {\n      \"fabr"..., 694) = 693
read(3, "", 1)                          = 0
rt_sigaction(SIGINT, {SIG_DFL, [], SA_RESTORER, 0x7f57b2bc1390}, {0x442160, [], SA_RESTORER, 0x7f57b2bc1390}, 8) = 0
close(3)                                = 0
sigaltstack(NULL, {ss_sp=0x1f2b0b0, ss_flags=0, ss_size=8192}) = 0
sigaltstack({ss_sp=NULL, ss_flags=SS_DISABLE, ss_size=0}, NULL) = 0
exit_group(0)                           = ?
+++ exited with 0 +++

Similarly in the second file:

f = open('./myConfig.cfg')
jsonda = json.loads(f.read())
f.close()

The corresponding strace o/p is

open("./myConfig.cfg", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=693, ...}) = 0
ioctl(3, TCGETS, 0x7fff2e3f27c0)        = -1 ENOTTY (Inappropriate ioctl for device)
lseek(3, 0, SEEK_CUR)                   = 0
ioctl(3, TCGETS, 0x7fff2e3f2760)        = -1 ENOTTY (Inappropriate ioctl for device)
lseek(3, 0, SEEK_CUR)                   = 0
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=693, ...}) = 0
read(3, "{\n  \"osns\" : [\n    {\n      \"fabr"..., 694) = 693
read(3, "", 1)                          = 0
close(3)                                = 0
rt_sigaction(SIGINT, {SIG_DFL, [], SA_RESTORER, 0x7f4f52f6d390}, {0x442160, [], SA_RESTORER, 0x7f4f52f6d390}, 8) = 0
sigaltstack(NULL, {ss_sp=0x15130b0, ss_flags=0, ss_size=8192}) = 0
sigaltstack({ss_sp=NULL, ss_flags=SS_DISABLE, ss_size=0}, NULL) = 0
exit_group(0)                           = ?
+++ exited with 0 +++

Upvotes: 2

Views: 119

Answers (1)

godaygo
godaygo

Reputation: 2288

See this part of documentation Reading and writing files its fully answers your question, so "read the docs". I will provide a quote from there:

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.

Another part of the same section advocates to use with block, because in this case it is guaranteed to properly release the resources even if an exception is raised at some point (I think except SystemError):

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.

Eventually to answer your question - yes it is safe to assume that Python does an automatic close, but it is not safe to implicitly rely on.

Upvotes: 1

Related Questions