Python open檔案讀寫入模式說明

來源:互聯網
上載者:User

標籤:for   and   二進位   寫檔案   otherwise   進位   log   系統   開啟檔案   

  對於Python開啟檔案的模式,總是記不住,這次在部落格裡記錄一下

  r+: Open for reading and writing.  The stream is positioned  at  the beginning of the file.

  w+:Open for reading and writing.  The file is created  if  it  does not  exist, otherwise it is truncated.  The stream is positioned at the beginning of the file.

  r+具有讀寫屬性,從檔案頭開始寫,保留原檔案中沒有被覆蓋的內容;

  w+具有讀寫屬性,寫的時候如果檔案存在,會被清空,從頭開始寫。

 

  r 開啟唯讀檔案,該檔案必須存在。 
  r+ 開啟可讀寫的檔案,該檔案必須存在。 
  w 開啟唯寫檔案,若檔案存在則檔案長度清為0,即該檔案內容會消失。若檔案不存在則建立該檔案。 
  w+ 開啟可讀寫檔案,若檔案存在則檔案長度清為零,即該檔案內容會消失。若檔案不存在則建立該檔案。 
  a 以附加的方式開啟唯寫檔案。若檔案不存在,則會建立該檔案,如果檔案存在,寫入的資料會被加到檔案尾,即檔案原先的內容會被保留。 
  a+ 以附加方式開啟可讀寫的檔案。若檔案不存在,則會建立該檔案,如果檔案存在,寫入的資料會被加到檔案尾後,即檔案原先的內容會被保留。 
  上述的形態字串都可以再加一個b字元,如rb、w+b或ab+等組合,加入b 字元用來告訴函數庫開啟的檔案為二進位檔案,而非純文字檔案。不過在POSIX系統,包含Linux都會忽略該字元。

(轉自 http://blog.163.com/[email protected]/blog/static/17104630220101123113029840/)

Python open檔案讀寫入模式說明

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.