小编rah*_*chh的帖子

f.close(), f.close 和 f.closed 的区别

with open('new.txt', 'r+') as f:
    print(f.readline())


if f.close:
    print('It is closed')
else:
    print('It is open.')
Run Code Online (Sandbox Code Playgroud)

如果我运行此代码,输出'It is closed'. 但是,如果我将 if 语句从f.closeto更改为 ,则会f.closed()得到输出'It is open'。那么我的文件是关闭还是打开?为什么我得到不同的输出?

python file-io file with-statement python-3.x

0
推荐指数
1
解决办法
2989
查看次数

标签 统计

file ×1

file-io ×1

python ×1

python-3.x ×1

with-statement ×1