Sep 25, 2011

file in python

file object 支持 iterator protocal (有 __iter__ 函数),每一行循环

f = open('iterators.py','r')

for line in f:
    print line

还支持 context manager 协议,即可以用 with open() as f:
确保即使发生异常也会关闭file

0 comments: