import gzip
# 那是因为你调用了read方法,而这个方法会把文件一股脑儿读取出来的
# 为了便于你迭代,你可以在这里使用一个生成器
def read_gz_file(path):
if os.path.exists(path):
with gzip.open(path, 'rt') as pf:
for line in pf:
yield line
else:
print('the path [{}] is not exist!'.format(path))
con = read_gz_file('abc.gz')
if getattr(con, '__iter__', None):
for line in con:
print(line, end = '')