小编adr*_*ray的帖子

python中打开的文件描述符

当我在 IPython3 shell 中使用此代码时

 >>>data = open('file').read()
Run Code Online (Sandbox Code Playgroud)

然后检查打开的文件描述符:

 lsof | grep file
Run Code Online (Sandbox Code Playgroud)

我发现空列表

当我使用这个时:

>>>open('file')
Run Code Online (Sandbox Code Playgroud)

lsof显示两个项目。问题是为什么第一个操作关闭 fd 而第二个操作却没有?我认为垃圾收集器必须删除没有引用的文件对象。

当我重新分配值时,我知道解释器中的“_”var

>>>111
>>>_
111
Run Code Online (Sandbox Code Playgroud)

但描述符保持开放状态。当我重复

>>>open('file')
Run Code Online (Sandbox Code Playgroud)

n次有2*n个打开的描述符

python file python-3.x

4
推荐指数
1
解决办法
6851
查看次数

标签 统计

file ×1

python ×1

python-3.x ×1