python---檔案操作

來源:互聯網
上載者:User

標籤:分割線   重新整理   with   操作   內容   span   run   open   adl   

一、對檔案操作流程
1.開啟檔案,得到檔案控制代碼並賦值給一個變數
2.通過檔案控制代碼對檔案進行操作
3.關閉檔案


二、檔案開啟模式
f = open("file_test","r",encoding = "utf-8")    #開啟檔案,“讀模式”,只能讀,得到檔案控制代碼並賦值給一個變數print(f.read())                     #讀檔案所有內容,讀完之後檔案游標跳到最後,檔案大時慎用f.close()f1 = open("file_test","w",encoding="utf-8")     #寫入模式,會建立一個file_test檔案,再往裡寫入f1.write("-------------")f1.close()f2 = open("file_test","a",encoding="utf-8")     #追加模式,在原檔案內容最後追加,無原檔案則建立f2.write("-------------")f2.close()f3  = open("file_test","r+",encoding="utf-8")    #讀寫入模式,寫在原檔案內容最後追加,無原檔案則建立print(f3.readline())                  #按行讀print(f3.readline())print(f3.readline())                  #列印前三行,這個時候游標移動到第三行位置print(f3.tell())                    #列印游標位置f3.write("--------------")               #但是寫入還是檔案內容最後寫入f3.close()f4  = open("file_test","w+",encoding="utf-8")    #寫讀模式,,只要是“寫在前”都會建一個新檔案,在寫入f4.write("----------------------\n")f4.write("----------------------\n")f4.write("----------------------\n")f4.seek(10)                       #游標移動到10的位置f4.write("test4")                    #再寫入會將原內容覆蓋f4.seek(0)                        #將游標移動到開頭的位置print(f4.read())f4.close()#輸出#----------ni hao -----#----------------------#----------------------f5  = open("file_test","a+",encoding="utf-8")    #追加讀模式,在原檔案內容最後追加,無原檔案建立f5.write("----------------------\n")f5.write("----------------------\n")f5.write("----------------------\n")f5.seek(10)                       #游標移動到10的位置f5.write("test5")                    #再寫入會在檔案內容最後寫入f5.seek(0)print(f5.read())f5.close()f6  = open("file_test","rb")                 #以二進位檔案格式讀這個檔案print(f6.readline())print(f6.readline())print(f6.readline())f6.close()f7  = open("file_test","ab")                 #以二進位檔案格式追加這個檔案f7.write("-------------------\n".encode())       #encode 將str字元轉換為bytesf7.write("-------------------\n".encode())f7.write("-------------------\n".encode())f7.close()f8  = open("file_test","wb")                 #以二進位檔案格式寫這個檔案f8.write("-------------------\n".encode())       #encode 將str字元轉換為bytesf8.write("-------------------\n".encode())f8.write("-------------------\n".encode())f8.close()#註:還有rU或r+U模式,"U"表示在讀取時,可以將 \r \n \r\n自動轉換成 \n (與 r 或 r+ 模式同使用)

三、檔案迴圈

#按行迴圈,並且將第五行替換f = open("file_test","r",encoding="utf-8")count = 0for line in f:    count += 1    if count == 5:        print("----分割線----")        continue    print(line.strip())                           #strip是去除行首行尾的空格符和分行符號f.close()#f.readlines()                                    #切記用f.readlines是先將檔案轉換為列表,如果檔案太大時對記憶體消耗太大

四、檔案的修改

#開啟一個檔案,修改完了寫到一個新檔案f = open("file_test","r",encoding="utf-8")f_new = open("file_new","w",encoding="utf-8")for line in f:                               #按行取出,每行都是一串字串    if "fengxiaoli" in line:        line = line.replace("fengxiaoli","FENGXIAOLI")       #對字串進行操作,J    f_new.write(line)f.close()f_new.close()

五、flush方法

f = open("file_test","w",encoding="utf-8")f.write("hello\n")f.write("hello\n")f.write("hello\n")f.flush()       #當往檔案寫內容的時候,會有一個緩衝,達到一個時間,一次往檔案寫入。如果這時候斷電可能內容並沒有寫入成功,flush重新整理會立即執行

六、with語句

#with語句作用,為了避免開啟檔案後忘記關閉with open("file_test","r",encoding="utf-8") as f:          #類似於f = open("file_test","r",encoding="utf-8")with open("file_test","r",encoding="utf-8") as f, \        #還可以同時開啟多個檔案        open("file_test2","r",encoding="utf-8") as f2:

七、其他動作

f = open("file_test","r",encoding="utf-8")print(f.tell())              #列印游標位置,按字元計數print(f.readline())          #按行讀print(f.read(10))            #按字元讀print(f.tell())f.seek(0)                   #把游標回到開頭f.seek(12)                  #把游標移動到12個字元的位置print(f.readline())print(f.encoding)           #列印檔案編碼print(f.isatty())           #判斷檔案是否是終端裝置,返回Ture or falseprint(f.seekable())         #判斷是否能移動檔案游標,返回Ture or falseprint(f.readable())         #判斷檔案是否可讀print(f.writable())         #判斷檔案是否可寫# f  = open("file_test","a",encoding="utf-8")# f.truncate(12)      #從頭開始截取多少字元



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.