python 檔案的寫刪改

來源:互聯網
上載者:User

標籤:修改檔案   span   inner   檔案的   ==   ext1   指標   imp   寫入檔案   

 1 # coding=utf-8 2 # !/usr/bin/python 3 # -*- coding: UTF-8 -*- 4 import io 5 import os 6  7  8 def file_chance():                    #修改檔案內某一行的內容 9     f = open(‘text1‘, ‘r‘)10     w = open(‘text2‘, ‘w‘)11     n = 012     for i in f:13         n += 114         if n == 3:15             i = "yes,i am\n"16         w.write(i)17     f.close()18     w.close()19     os.remove(‘text1‘)20     os.rename(‘text2‘, ‘text1‘)21 22 def file_del():                       #刪除檔案內容23     f = open(‘text1‘,‘w‘)24     f.close()25 26 def file_write():                     #檔案寫入20行you are a winner27     f = open(‘text1‘, ‘w‘)28     for i in range(20):29         f.write(‘you are a winner\n‘)30     f.close()31 32 33 file_del()34 file_write()35 file_chance()36 ‘‘‘37 總結:38 39 "i=‘ ‘.join([‘123456‘])" = "i+‘123456’"40 f.tell() 指標的位置41 f.read(*) 讀取*個字元,中文佔3個位置,英文佔一共位置42 f.seek(*) 改變指標的位置43 r+模式 tell在最末尾的位置44 w+模式 tell在第一位置且對象內容為空白45 a+模式 tell在末尾位置46 寫/刪:w/w+/a/a+ 47 改:讀取一個檔案,寫入另一個檔案,將讀取檔案刪除,將寫入檔案重新命名為讀取檔案48 ‘‘‘

 

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.