只是简单实现 删除文本最后一行。网上搜索到的要么太长,生硬难以理解。要么语法过期Python3无效
## python3
import os
def delLastLine(path):
with open(path, "rb+") as f:
lines = f.readlines() # 读取所有行
last_line = lines[-1] # 取最后一行
for i in range(len(last_line) + 2): ##愚蠢办法,但是有效
f.seek(-1, os.SEEK_END)
f.truncate()
return
if __name__ == '__main__':
try:
delLastLine('test.txt')
except:
pass