filename = 'pi_digits.txt' with open(filename) as f:#默认以只读方式打开文件 lines = f.readlines()#读取所有行,结果为列表,每行为列表一元素 for line in lines: print(line.rstrip()) with open(filename) as f:#默认以只读方式打开文件 lines = f.read()#读取所有内容给变量,结果是一串字符 print(lines) for line in line