python3 文件迭代器

来源:互联网 发布:最后一次网络歌手 编辑:程序博客网 时间:2024/06/14 22:07
file_path =r'C:\Users\Administrator\Desktop\pwd.txt'for line in open(file_path):  #Use file iterators to read by lines    print(line, end='')       #Calls __next__, catches Stoplteration

逐行读取文本文件的最佳方式就是根本不要去读取,使用for循环在每轮自动调用next从而前进到下一行。

这是最简单的写法,运行最快,并且从内存使用的情况来说也是最好的。

0 0
原创粉丝点击