标签:
with open(r‘somefileName‘) as somefile: for line in somefile: print line # ...more code
这里使用了 with 语句,不管在处理文件过程中是否发生异常,都能保证 with 语句执行完毕后已经关闭了打开的文件句柄。如果使用传统的 try/finally 范式,则要使用类似如下代码:
somefile = open(r‘somefileName‘) try: for line in somefile: print line # ...more code finally: somefile.close()
标签:
原文地址:http://www.cnblogs.com/fengyr/p/5198202.html