qiezijiajia

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

要读取个大文件,文件大概是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.

posted on 2017-07-25 17:33  qiezijiajia  阅读(188)  评论(0编辑  收藏  举报