標籤:修改檔案 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 檔案的寫刪改