1 # 写方法1 2 f = open('tmp.txt','w') 3 f.write('hello world') 4 f.close() 5 6 # 写方法2 7 with open('tmp.txt','w') as f: 8 f.write('hello \n word') 9 10 # 读11 with open('tmp.txt', 'r') as f:12 print(f.read())13 14 # 逐行读15 with open('tmp.txt','r') as f:16 for line in f.readlines():17 print(line.strip())#strip()去除空行