十四、python沉澱之路--檔案操作

來源:互聯網
上載者:User

標籤:pen   位元組   adl   while   報錯   pytho   ati   utf-8   檢查   

一、檔案操作b模式

1、

1 # f = open(‘test11.py‘,‘rb‘,encoding=‘utf-8‘)  # 這種情況會報錯2 f = open(‘test11.py‘,‘rb‘)    # b 模式不能指定編碼方式3 data = f.read()4 print(‘直接列印出來:‘,data)5 print(‘解碼列印\n‘,data.decode(‘utf-8‘))6 f.close()
1 直接列印出來: b‘11111\r\n22222\r\n3333\r\n‘2 解碼列印3  111114 222225 3333

總結:字串 -------encoding ------->> bytes
           bytes --------decode --------->> 字串

例2

 

 1 f = open(‘test22.py‘,‘wb‘) 2 f.write(bytes(‘99999\n‘,encoding=‘utf-8‘)) 3 f.write(‘北京‘.encode(‘utf-8‘)) 4 f.close() 5  6 f1 = open(‘test22.py‘,‘br‘) 7 data = f1.read() 8 print(‘原碼列印‘,data) 9 print(‘解碼列印\n‘,data.decode(‘utf-8‘))10 f1.close()

 

1 原碼列印 b‘99999\n\xe5\x8c\x97\xe4\xba\xac‘2 解碼列印3  999994 北京

註:原碼列印和解碼列印的區別,寫檔案前,即在開啟的時候不能指定編碼格式。只有在寫的時候才指定編碼格式。

二、檔案操作

1、

 

 

1 f = open(‘練習.txt‘,"r+",encoding=‘utf-8‘)2 print(f.read())3 f.write(‘1234556789\n‘)4 print(f.read())5 f.close()

 

1 1234556789

2、closed,encoding, flush(),readlinse() 的使用

1 f = open(‘練習.txt‘,"r+",encoding=‘utf-8‘,newline=‘‘)2 print(f.closed)     # closed 判斷檔案是否關閉3 print(f.encoding)   # 檢查是哪種編碼格式4 f.flush()           # 寫完內容後是暫時存在了緩衝裡面,通過flush()函數沖刷一下,就將內容儲存在了記憶體、# f.flush() #講檔案內容從記憶體刷到硬碟5 print(f.readlines())  #將所有內容以列表形式列印出來,
1 False2 utf-83 [‘\r\n‘, ‘1234556789\r\n‘, ‘1234556789\r\n‘]

3、tell的使用,列印當前所在位置

 

 

1 f = open(‘練習.txt‘,"r+",encoding=‘utf-8‘,newline=‘‘)2 print(f.tell())3 print(f.readline())     #  因為12345 後還有\r\n,沒有顯示出來4 print(f.tell())

 

4、seek的使用,(是以位元組為單位的)

 

 

 1 f = open(‘練習.txt‘,"r+",encoding=‘utf-8‘,newline=‘‘) 2 f.write(‘my name is cainiao who is from earth‘) 3 f.close() 4  5 f1 = open(‘練習.txt‘,‘r+‘,encoding=‘utf-8‘) 6 print(f1.read()) 7  8 f1.seek(3,0)  #“0”代表從檔案開頭開始位移,位移3個單位 9 print(f1.tell())10 print(f1.read(3))11 print(f1.read(6))  #從位移之後的指標所指的位置,開始讀取6個字元12 print(f1.tell())13 14 print(f1.readline())15 # print(‘看看是否到了最後:‘,f1.read(5))16 17 print(f1.seek(5,0))18 print(f1.seek(3,1))19 print(f1.seek(0,2))  #“2”代表從末尾算起,“0”代表位移0個單位

 

1 nam2     print(f1.seek(3,1))3 e is c4 io.UnsupportedOperation: can‘t do nonzero cur-relative seeks5 126 ainiao who is from earth7 5

5、讀取大檔案中最後一行

 1 f=open(‘練習.txt‘,‘rb‘) 2 for i in f: 3     offs=-10 4     while True: 5         f.seek(offs,2) 6         data=f.readlines() 7         if len(data) > 1: 8             print(‘檔案的最後一行是%s‘ %(data[-1].decode(‘utf-8‘))) 9             break10         offs*=2

 

十四、python沉澱之路--檔案操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.