Python学习笔记025

梦想与她 提交于 2020-03-12 08:36:14

文件读写模式

# r+ 表示读和写,光标在起始位置,读操作默认从0开始,写操作默认从末尾开始f=open('poem8','r+',encoding='utf8')print(f.tell())print(f.readline())# f.seek(0)# .write默认添加在末尾f.write('\nHuawei')f.close()
# w+ 表示写和读,默认清空文件内容f=open('poem8','w+',encoding='utf8')print(f.readline())# f.seek(0)# .write默认添加在末尾f.write('Huawei')# 光标默认在末尾,后面内容为空print(f.readline())f.close()
# a+ 表示追加,光标默认在末尾f=open('poem8','a+',encoding='utf8')print(f.tell())print(f.readline())# f.seek(0)# .write默认添加在末尾f.write('\nHuawei')f.close()同时管理多个文件
# with打开的文件,自动关闭# f=open('poem8','r+',encoding='utf8')with open('poem8','r') as f:    f.readline()    f.read()print()# with同时管理多个文件# f_read=open('poem8','r',encoding='utf8')# f_write=open('poem9','w',encoding='utf8')with open('poem8','r') as f_read, open('poem9','w') as f_write:    for line in f_read:        f_write.write(line)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!