1
我試圖根據用戶輸入從終端寫入或追加數據到具有一些數據的文件。我的代碼如下所述,它允許我添加/附加,但不能作爲新行。我試圖解決它,但無法弄清楚,我做錯了什麼?python 3.x:如何按照新行中的用戶輸入將文本寫入/附加到現有文件?
f = open("test1.txt","a+");
print ("Name of the file:",f.name);
#rdl = f.readline();
#print ("This is my readline : %s" % rdl);
#f.seek(0,2);
usrtext = input("Enter your text which you want to add : " + "\n");
f.seek(0,2);
f.write(usrtext);
f.seek(0,0);
rlds = f.readlines();
print ("This is my readlines : %s\n" % rlds);
#Close opened file
f.close();
預期輸出:
This is my new line 1
This is my append/write line