python的with ---as 的用法
with EXPRESSION [ as VARIABLE] :
WITH-BLOCK
这个语法是用例代替传统的try .... finally语法的
file = open("/tmp/foo.txt")
try:
data = file.read()
finally:
file.close()
使用with...as...的方式替换
with open("/tmp/foo.txt") as file:
data = file.read()
网友评论