床前明月光,疑是地上霜
舉頭望明月,低頭思故鄉
f = open('libai',encoding = 'utf-8')
print(f.read()) 床前明月光,疑是地上霜 舉頭望明月,低頭思故鄉
f = open('libai',encoding = 'utf-8')
print(f.readline()) 床前明月光,疑是地上霜
f = open('libai',encoding = 'utf-8')
print(f.readlines()) ['床前明月光,疑是地上霜\n', '舉頭望明月,低頭思故鄉']
f = open('libai',encoding='utf-8') data = f.read() #預設游標在起始的位置,read()讀取完後,游標停留到檔案末尾
data2 = f.read() #data2讀取的內容為空
print(data)
print(data2) f.close() #關閉檔案
檔案
abcdefg
hyjklmn
opqrstu
vwxyz
with open('libai') as f:
print(f.read())
#rb 二進位制樣式讀取
f = open('libai','rb')
#主要用於網路傳輸print(f.readline())
b’\xe5\xba\x8a\xe5\x89\x8d\xe6\x98\x8e\xe6\x9c\x88\xe5\x85\x89\xef\xbc\x8c\xe7\x96\x91\xe6\x98\xaf\xe5\x9c\xb0\xe4\xb8\x8a\xe9\x9c\x9c\r\n’
f = open('libai','wb') f.write('中文'.encode())