参考:http://zhoutall.com/archives/325
开了文件就得关,
用 with as 就省了 finally
try
:
with
open
(
"a.txt"
) as f :
do something
except
xxxError:
do something about exception
with-as表达式极大的简化了每次写finally的工作,这对保持代码的优雅性是有极大帮助的。