写入现有.txt文件 - Escribir en archivos .txt ya existentes.



为什么一开始写入.txt文件会删除现有字符? 对不起,我的无知,我已经很久没有编程了。

Alabama[edit]
Auburn (Auburn University)[1]
Florence (University of North Alabama)
Jacksonville (Jacksonville State University)[2]
Livingston (University of West Alabama)[2]
Montevallo (University of Montevallo)[2]
Troy (Troy University)[2]
Tuscaloosa (University of Alabama, Stillman College, Shelton State)[3][4]
Tuskegee (Tuskegee University)[5]
f =open('university_towns.txt',"r+")
f.write('Tn')
f.close()
T
bama[edit]
Auburn (Auburn University)[1]
Florence (University of North Alabama)
Jacksonville (Jacksonville State University)[2]
Livingston (University of West Alabama)[2]
Montevallo (University of Montevallo)[2]
Troy (Troy University)[2]
Tuscaloosa (University of Alabama, Stillman College, Shelton State)[3][4]
Tuskegee (Tuskegee University)[5]

要附加到文件,您需要使用带有"a+"而不是"r+"的"open"(见这里(

所以在您的情况下:

f =open('university_towns.txt',"a+")
f.write('Tn')
f.close()

最新更新