第9類:檔案與異常處理

TQC+ 程式語言Python 903 成績資料

題目說明:

請撰寫一程式,要求使用者輸入五個人的名字並加入到data.txt的尾端。之後再顯示此檔案的內容。

範例輸入

Daisy
Kelvin
Tom
Joyce
Sarah

範例輸出

Append completed!
Content of "data.txt":
Ben
Cathy
Tony
Daisy
Kelvin
Tom
Joyce
Sarah
 

檔案下載:
data.txt


程式碼:

file = open("data.txt", 'a+')

for i in range(5):
    name = input()
    file.write('n' + name)

print("Append completed!")
print('Content of "data.txt":')

file.seek(0)
print(file.read())

file.close()

TQC+ 程式語言Python 第9類:檔案與異常處理





如果覺得文章內容還不錯的話,麻煩請幫我點個讚!感謝

可以多點幾次喔~~

第一次點讚需使用 Google 或 Facebook 帳號註冊

發表迴響取消回覆

Exit mobile version