python之檔案操作

來源:互聯網
上載者:User

標籤:python

#read 無參數,讀全部,有參數,有b按照位元組讀,無b按字元讀#tell,擷取當前指標的位置#seek,跳轉指標的位置#write,往裡寫資料,有b寫位元組,無b寫字元#close,關閉檔案#fileno 檔案描述符#flush,強制把寫的內容刷到硬碟裡#read#readable,是否可讀#writeable,是否可寫#seekable,是否可以移動指標#readline,唯讀一行#truncate,會把指標後面的內容全部幹掉,截斷檔案#for迴圈檔案對象,迴圈每一行#with open(‘db‘) as f:  2.7之後,支援同時開啟兩個檔案,with open(‘db‘) as f1,with open(‘db‘,r) as f2,比如把一個檔案的內容#同時寫入到另外一個檔案中# with open(‘db‘,‘r‘,encoding=‘utf-8‘) as f1,open(‘db_bak‘,‘w‘,encoding=‘utf-8‘) as f2:#     n = 0#     for line in f1:#         if n <= 2:#             f2.write(line)#             n += 1#         else:#             break#             print(‘over‘)#通過with把f1中的123替換為ABC# with open(‘db‘,‘r‘,encoding=‘utf-8‘) as f1,open(‘db_bak‘,‘w‘,encoding=‘utf-8‘) as f2:#     for line in f1:#         new = line.replace(‘123‘,‘ABC‘)#         f2.write(new)


本文出自 “百年民國” 部落格,請務必保留此出處http://bainianminguo.blog.51cto.com/4572772/1945166

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.