# 只读 fo = open('test.txt') content = fo.read() fo.flush() # 提交更新,可有可无,如果急就用这个方法,不着急就关闭后生效 fo.close() print(content) # 写 fo = open('test.txt', 'w+') fo.write('xixi\n') fo.close() #后面拼接 fo = open('test.txt', 'a+') fo.write('a\n') # 单个 fo.write('b\n') fo.write('c\n') fo.writelines(['aaa\n','bbb\n', 'ccc\n']) # 多个字符 fo.write('hello\n') fo.close() # 按照行获取,返回集合 fo = open('test.txt') print(fo.readlines()) fo.close() # 读取下一行 fo = open('test.txt') #print(fo.next()) # 旧版 print(fo.__next__()) #新版 print(fo.__next__()) print(fo.__next__()) fo.seek(0, 0) # 移动光标,重头读取 print(fo.__next__()) fo.close()