Python學習day7檔案操作

來源:互聯網
上載者:User

標籤:字串   str   類型   bsp   mode   tab   文本   檔案   情況   

文本操作# 讀取utf-8編碼資料,【轉換】成unicode(str)編碼的資料# obj = open(‘D:\美女模特空姐護士連絡方式.txt‘,encoding=‘utf-8‘,mode=‘r‘)# content = obj.read()# obj.close()# print(content,type(content))# obj = open(‘D:\美女模特空姐護士連絡方式.txt‘,mode=‘rb‘)# content = obj.read()# obj.close()# print(content,type(content))# 1. 檔案在硬碟上時,一定是:utf-8,gbk......# 2. rb, bytes,                  rb# 3. r,  轉換encoding=utf-8      r   encoding:utf-8# 讀檔案# obj = open(‘D:\\n美女模特空姐護士連絡方式.txt‘,encoding=‘utf-8‘,mode=‘r‘)# content = obj.read() # 字串類型# obj.close()# print(content,type(content))# obj = open(‘D:\美女模特空姐護士連絡方式.txt‘,mode=‘rb‘)# content = obj.read() # bytes類型# obj.close()# print(content,type(content))# obj = open(‘log‘,mode=‘rb‘)# data = obj.read()# obj.close()## obj = open(‘log‘,mode=‘r‘,encoding=‘utf-8‘)# data = obj.read()# obj.close()# print(data)# obj = open(‘美女.txt‘,mode=‘w‘,encoding=‘utf-8‘)# obj.write("海角-賈敏")# obj.close()# obj = open(‘美女.txt‘,mode=‘wb‘)# obj.write("海角-賈敏22222".encode(‘utf-8‘))# obj.close()# obj = open(‘美女.txt‘,mode=‘a‘,encoding=‘utf-8‘)# obj.write("小紅")# obj.close()# obj = open(‘美女.txt‘,mode=‘ab‘)# data = obj.read()# obj.close()## print(data)# obj = open(‘美女.txt‘,mode=‘r+‘,encoding=‘utf-8‘)# data = obj.read(1) # 讀# # 情況一:# # obj.write(‘6‘) # 在最後繼續寫# # 情況二:# # obj.seek(1)# # obj.write(‘6‘) # 定位到指定位置# obj.close()# print(data)# obj = open(‘美女.txt‘,mode=‘r+‘,encoding=‘utf-8‘)#obj = open(‘美女.txt‘,mode=‘r+‘,encoding=‘utf-8‘)# data = obj.read()  # 讀取所有檔案內容# data = obj.read(2) # r,字元;rb,位元組# obj.write(‘位元組或字串‘)# obj.seek(2)         # 永遠是位元組,指定指標位置# obj.tell()          # 讀取當前指標的位置# 是否可寫# obj.writable()# 是否可讀# obj.readable()# 將記憶體中內容刷到硬碟# obj.flush()# obj.tell()# data = obj.readline()# print(data)# data = obj.readline()# print(data)# 截取檔案內容,根據指標位置,只保留指標之前資料# obj.truncate()# for line in obj:#     print(line)obj.close()

 

Python學習day7檔案操作

聯繫我們

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