1. 程式人生 > >小白python學習——檔案的讀入和書寫

小白python學習——檔案的讀入和書寫

1.開啟檔案(這個檔案是pycharm中建立的txt)

filename='LOVE.txt'
with open(filename) as file:
    files=file.readlines()
for i in files:
    print(i)
ZY I Love YOU


We have know each other a long time
在with程式碼塊中開啟檔案,readlines函式是把txt檔案中打包成列表

2.直接建立檔案和在檔案中讀入字串

filename='LOVE.txt'
with open(filename,'w') as 
file: file.write("YOU LOVE ME")

 
YOU LOVE ME

'w'是寫入的形式,書上很多種,可以去看,運用的函式是write函式