码迷,mamicode.com
首页 > 编程语言 > 详细

python 读取大文件

时间:2017-07-25 18:01:22      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:and   rabl   handle   lan   tomat   cal   files   for   buffere   

要读取个大文件,文件大概是3G左右,担心read会出现内存溢出的情况,网上找了个靠谱的用法:

with open(...) as f:
    for line in f:
        <do something with line>

The with statement handles opening and closing the file, including if an exception is raised in the inner block. The for line in f treats the file object f as an iterable, which automatically uses buffered IO and memory management so you don‘t have to worry about large files.

There should be one -- and preferably only one -- obvious way to do it.

python 读取大文件

标签:and   rabl   handle   lan   tomat   cal   files   for   buffere   

原文地址:http://www.cnblogs.com/dpains/p/7235430.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!