Python學習Day8—檔案基本操作

來源:互聯網
上載者:User

標籤:絕對路徑   ada   python   不用   bytes   call   odi   單位   lan   

開啟檔案
    #’filename.txt‘處指定檔案路徑,可以使用絕對路徑和相對路徑    #mode=‘w‘指定檔案的開啟檔案    #encoding=‘utf-8‘指定檔案編碼f = open(‘filename.txt‘,mode=‘w‘,encoding=‘utf-8‘)f.close() #關閉檔案,使用上面控制代碼開啟檔案後,檔案會一直在記憶體中運行,在對檔案進行操作後,應記得關閉檔案---------------------------------------------------------------------------#使用with關鍵字+open開啟檔案後,不用再使用close對檔案進行關閉,並且可以同時開啟多個檔案,開啟多個檔案需要用逗號隔開,最後的控制代碼名稱後需要加冒號with open(‘filename.txt‘,mode=‘w‘,encoding=‘utf-8‘) as f:#開啟一個檔案with open(‘filename.txt‘,mode=‘w‘,encoding=‘utf-8‘) as f,with open(‘filename.txt‘,mode=‘w‘,encoding=‘utf-8‘) as f1:#開啟多個檔案
操作檔案1.讀
# 此處filename.txt檔案中原始內容為:人生苦短,我用python# ①r——唯讀,不存在報錯with open(‘filename.txt‘, mode=‘r‘, encoding=‘utf-8‘) as f:    content = f.read()    print(content)>>> 人生苦短,我用python# ②rb——以bytes類型讀with open(‘filename.txt‘, mode=‘rb‘) as f:    content = f.read()    print(content)>>> b‘\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8python‘# ③r+——讀寫,不存在不會建立,寫會覆蓋之前內容with open(‘filename.txt‘, mode=‘r+‘,encoding=‘utf-8‘) as f:    content = f.read()    f.write(‘\n新添加:Life is short , I use python‘)    print(content)>>> 人生苦短,我用python# ④target_file.txt中的內容為:>>> 人生苦短,我用python>>> 新添加:Life is short , I use python# ⑤r+b——以bytes類型讀寫with open(‘filename.txt‘, mode=‘r+b‘) as f:    content = f.read()    f.write(‘\n新添加:Life is short , I use python‘.encode(‘utf-8‘))    print(content)>>> b‘\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8python‘# target_file.txt中的內容為:>>> 人生苦短,我用python>>> 新添加:Life is short , I use python
2.寫
# 此處filename.txt檔案中原始內容為空白,需要向其中寫入:人生苦短,我用python# ①w——唯寫,不存在則建立,存在則清空再寫with open(‘filename.txt‘, mode=‘w‘, encoding=‘utf-8‘) as f:    content = f.write(‘人生苦短,我用python‘)# target_file.txt中的內容為:>>> 人生苦短,我用python# ②x——唯寫,不存在則建立,存在則報錯with open(‘filename.txt‘, mode=‘x‘, encoding=‘utf-8‘) as f:    content = f.write(‘人生苦短,我用python‘)# target_file.txt存在:>>> Traceback (most recent call last):>>>    File "D:/python_fullstack_s9/day8/practice.py", line 94, in <module>>>>      with open(‘target_file.txt‘, mode=‘x‘, encoding=‘utf-8‘) as f:>>>  FileExistsError: [Errno 17] File exists: ‘target_file.txt‘# filename.txt不存在:>>> 人生苦短,我用python# # ③wb——以bytes類型寫with open(‘filename.txt‘, mode=‘wb‘) as f:    content = f.write(‘人生苦短,我用python‘.encode(‘utf-8‘))>>> 人生苦短,我用python# # ④w+——寫讀,不存在則建立,寫會覆蓋之前的內容with open(‘filename.txt‘, mode=‘w+‘) as f:    content = f.write(‘hello,world‘)    date = f.read()    print(date)>>># filename.txt中的內容為:>>> hello,world# # ⑤w+b——以bytes類型寫讀with open(‘filename.txt‘, mode=‘w+b‘) as f:    content = f.write(‘hello,world‘.encode(‘utf-8‘))    date = f.read()    print(date)>>> b‘‘# filename.txt中的內容為:>>> hello,world
3.追加
# 此處filename.txt檔案中原始內容為:人生苦短,我用python,需要在後面添加‘誰用誰知道‘內容# ①a——追加,不存在則建立,存在則追加with open(‘filename.txt‘, mode=‘a‘, encoding=‘utf-8‘) as f:    content = f.write(‘誰用誰知道‘)filename.txt中的內容為:>>> 人生苦短,我用python誰用誰知道# ②ab——以bytes類型追加with open(‘filename.txt‘, mode=‘ab‘) as f:    content = f.write(‘誰用誰知道‘.encode(‘utf-8‘))>>> 人生苦短,我用python誰用誰知道# ③a+——可讀可寫,不存在則建立,寫則追加with open(‘filename.txt‘, mode=‘a+‘,encoding=‘utf-8‘) as f:    content = f.write(‘誰用誰知道‘)    f.seek(0)    date = f.read()    print(date)>>> 人生苦短,我用python誰用誰知道# ④a+b——以bytes類型可讀可寫with open(‘filename.txt‘, mode=‘a+b‘) as f:    content = f.write(‘誰用誰知道‘.encode(‘utf-8‘))    f.seek(0)    date = f.read()    print(date)>>> b‘\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8python\xe8\xb0\x81\xe7\x94\xa8\xe8\xb0\x81\xe7\x9f\xa5\xe9\x81\x93‘# filename.txt中的內容為:>>> 人生苦短,我用python誰用誰知道
4.其他動作① seek() 移動游標指標位置

seek有三種移動方式0,1,2,其中1和2必須在b模式下進行,但無論哪種模式,都是以bytes為單位移動的

② tell() 返回當前指標所在的位置

tell對於英文字元就是佔一個,中文字元佔三個,參數表示的是位元組數區分與read()的不同.

③ truncate() 截斷檔案

truncate是截斷檔案,所以檔案的開啟檔案必須可寫,但是不能用w或w+等方式開啟,因為那樣直接清空檔案了,所以truncate要在r+或a或a+等模式下測試效果

④ readline() 讀取一行⑤ readlines() 讀取多行,返回為列表⑥ readable() 檔案是否可讀⑦ writeline() 寫入一行⑧ writelines() 寫入多行⑨ writable() 檔案是否可讀⑩ closed() 檔案是否關閉? encoding=’utf-8’ 如果檔案開啟模式為b,則沒有該屬性? flush() 立刻將檔案內容從記憶體刷到硬碟? for迴圈檔案控制代碼
with open(‘filename.txt‘, mode=‘r‘,encoding=‘utf-8‘) as f:    for i in f:        print(i)>>> 人生苦短,我用python>>> 誰用誰知道

 

Python學習Day8—檔案基本操作

聯繫我們

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