0
現在我想在文件中添加一些單詞,但是我只能通過使用f.open('xxx.txt')在文件末尾添加一些單詞(.txt) ,'a') 如果我使用'a +'模式,這些詞將從頭開始發生。我希望在第一行插入一些句子而不用改變原來的句子。如何在txt文件中插入一些單詞
# using python 2.7.12
f = open('Bob.txt','w')
f.write('How many roads must a man walk down'
' \nBefore they call him a man'
' \nHow many seas must a white dove sail'
' \nBefore she sleeps in the sand'
' \nHow many times must the cannon balls fly'
" \nBefore they're forever banned"
" \nThe answer my friend is blowing in the wind"
" \nThe answer is blowing in the wind")
f.close()
# add spice to the song
f1 = open('Bob.txt','a')
f1.write("\n1962 by Warner Bros.inc.")
f1.close()
# print song
f2 = open('Bob.txt','r')
a = f2.read()
print a
我希望在第一行插入「Bob Dylan」,我應該添加什麼代碼?