我正在使用Python,并且希望在不删除或复制文件的情况下将字符串插入到文本文件中。我该怎么做呢?
发布于 2008-09-24 06:35:51
这取决于你想做什么。要追加,可以用“a”打开它:
 with open("foo.txt", "a") as f:
     f.write("new line\n")如果你想预先准备一些东西,你必须先从文件中读取:
with open("foo.txt", "r+") as f:
     old = f.read() # read everything in the file
     f.seek(0) # rewind
     f.write("new line\n" + old) # write the new line beforehttps://stackoverflow.com/questions/125703
复制相似问题