你可以检查
file objects
并看到要从末尾插入元素,我们可以使用seek函数的第二个参数,然后获取输出。
with open("text.txt","w") as f:
f.write("Hello, how are you? \n\n\n")
f.seek(-2,2)
f.write("I am Sam.")
with open("text.txt","r") as f:
print(repr(f.read()))
产量
'Hello, how are you? \nI am Sam.'