今天是30天程式語言研究的第九天,研究的語言一樣是python,今天主要學習的是檔案的寫入和寫出
網址:https://www.youtube.com/watch?v=zdMUJJKFdsU&ab_channel=GrandmaCan-%E6%88%91%E9%98%BF%E5%AC%A4%E9%83%BD%E6%9C%83
筆記:
#讀取
file = open('123.txt', 'r')
print(file.readlines())
print(file.read()) #也可以寫成for line in file:
# print(line)
print(file.readline())
file.close()
#覆寫
file = open('123.txt', 'w')
file.write('HIHI')
file.close()
file = open('123.txt', 'w')
file.write('77\n78\n79\n80\n')
file.close()
file = open('123.txt', 'a') # 也可以寫成以下這樣,離開with 就會自動file.colse()了
file.write('大家好')
file.close()
with file = open('123.txt', 'a') as file:
file.write('大家好')