我有一个file.txt,其中包含文本12345
(总共5个字节)。
我想删除3
并将45
左移一个字节,总共我想有1245
(总共4个字节)
这是我的密码:
with open ('a.txt', "r+b") as fl:
fl.seek(0)
onetwo = fl.read(2)
fl.seek(3)
fourfive = fl.read(2)
fl.seek(0)
fl.write(onetwo+fourfive)
但结果我得到了这样的结论:
12455
我试图截断,但截断无助于解决我正在处理的文件(不是此文件)
Tags:
您必须编写一个新文件:
然后移动文件以覆盖:
移位字节后,使用
truncate
方法收缩文件实际上,您可能不会使用硬编码的偏移量,但这证明了这一点
您需要重写文件,但无法以您尝试的方式完成。我试过这个,它成功了:
相关问题 更多 >
编程相关推荐