首页
it技术
登录
6mi
u
盘
搜
搜 索
it技术
with ... as 读写文件
with ... as 读写文件
it
2023-05-09
75
python使用with as 读写文件
Python引入了with语句来自动帮我们调用close()方法,同时也解决了异常问题
with
open
(
'test.txt'
,
'r'
)
as
f
:
print
(
f
.
read
(
)
)
等价于
try
:
f
=
open
(
'test1.txt'
,
'r'
)
print
(
f
.
read
(
)
)
finally
:
f
.
close
(
)
转载请注明原文地址: https://lol.8miu.com/read-4489.html
最新回复
(
0
)