读取文件时使用 with 语句,因为文件将在处理后关闭。逐行阅读:with open("textfile.txt", "r") as f: for line in f: print(line)阅读所有行,然后遍历该行:with open("textfile.txt", "r") as f2: lines = f2.readlines()for ln in lines: print(ln)