python使用with无需显示关闭文件

一般采用显示关闭文件:

f = open('./weibo.py','r')

print f.read()

f.close()

 

with open('./weibo.py') as f:
    for line in f:
        print line
退出with代码块时文件自动关闭,不需显示关闭。

posted @ 2015-06-05 14:39  清风2014  阅读(1294)  评论(0编辑  收藏  举报