使用open语句打开一来个文件,此时调用的是w写入模式,下面使用read是没有权限的。
使用write写入,但是此时并没有真正的写入,而是还存在与内存中。此时执行read读取的为空字符。
with open(data_path,'r',encoding='utf-8') as up:
pb=up.read()
with open(data_path,'r',encoding='utf-8') as vp:
head, sep, tail = pb.partition('————')
vp.write(head)
up.close()
vp.close()
把‘r'换成’w‘即可
with open(data_path,'r',encoding='utf-8') as up:
pb=up.read()
with open(data_path,'w',encoding='utf-8') as vp:
head, sep, tail = pb.partition('————')
vp.write(head)
up.close()
vp.close()
读写中后面的参数:w, r, wt, rt,wb,rb ,都是 python 里面文件操作的模式 。
1、w是写模式 ,r是读 模式 。
2、t是windows平台特有的所谓text mode (文本模式),区别 在于会自动识别windows平台的换行符。
在Python中遇到`io.UnsupportedOperation: not readable`错误通常是由于尝试在写入模式下读取文件导致的。示例代码展示了如何在正确模式下进行读写操作,强调了'r'和'w'模式的区别,并提醒注意't'模式在Windows平台上的换行符处理。解决方法是确保在写入后切换到读取模式,或者反之。
最低0.47元/天 解锁文章

5万+

被折叠的 条评论
为什么被折叠?



