Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想编辑文件,我发送的新文本将在第一行,而不是底部。我用这个:
with open("OUTPUT.txt", "a") as myfile: myfile.write("row #2 text") myfile.write("\nrow #1 text")
但它只会将文本添加到新行。
谢谢
解决了!
使用 file('filename', 'r') 作为原始:data = original.read() 使用 file('filename', 'w') 作为修改:modified.write("new first line\n" + data)