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从而前进到下一行。
这是最简单的写法,运行最快,并且从内存使用的情况来说也是最好的。