def iter_file(path, size=1024): with open(path, "rb", ) as f: for data in iter(lambda: f.read(size), b''): yield data for row in iter_file("manage.py"): print(row)
—— 用迭代器去读取文件中的内容,防止文件太大一次性读取的话占用过大内存!
下面这样一次性读取的方式不可取
with open("manage.py", "r") as f: print(f.read())
——