首页 > 代码库 > python 生成器按指定大小读取文件

python 生成器按指定大小读取文件

#!/usr/bin/env python

import os
import sys

def read_file(fpath):
  Block_Size = 1024
  with open(fpath,"r") as f:
    while True:
      block = f.read(Block_Size)
      if block:
        yield block
      else:
        return

for i in read_file(sys.argv[1]):
  print(i)

python 生成器按指定大小读取文件