python会在文件出来后关闭它吗?

2024-06-09 05:00:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我厌倦了两个代码示例,一个是close,另一个是outclose,strace说这两个代码都调用了.close()

了解python3.6的自动关闭功能是否安全:

表1: 文件中的行是:

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

相应的策略o/p为

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 +++

类似地,在第二个文件中:

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

相应的策略o/p为

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 +++

Tags: closereadsizemodeseekopencfgnull
1条回答
网友
1楼 · 发布于 2024-06-09 05:00:45

请参阅文档的这一部分Reading and writing files它完全回答了您的问题,因此请“阅读文档”。我将从中引用:

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.

同一节的另一部分主张使用with块,因为在这种情况下,即使在某个时刻引发异常(我认为除了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.

最终回答您的问题-是的,假设Python执行自动关闭是安全的,但是隐式地依赖它是不安全的。你知道吗

相关问题 更多 >