Tuesday, June 19, 2012

[Python] with语句

with statement in Python

http://effbot.org/zone/python-with-statement.htm
with open("foo.txt") as f:
    for line in f:
        print line
        # do sth else
使用with来打开文件的好处是, 不用在调用f.close()

No comments: