python的檔案操作方法匯總,

來源:互聯網
上載者:User

python的檔案操作方法匯總,

檔案的讀操作

樣本:

 print("->檔案控制代碼的擷取,讀操作:")  f = open('無題','r',encoding='utf8') d = f.read() f.close() print(d)  print('->例二:') f = open('無題','r',encoding='utf8') e = f.read(9) f.close() print(e) #python3中,檔案中一個中英文都佔位1

運行結果:

複製代碼

->檔案控制代碼的擷取,讀操作:昨夜星辰昨夜風畫樓西畔桂堂東身無彩鳳雙飛翼心有靈犀一點通->例二:昨夜星辰昨夜風畫

檔案的寫操作

知識點:

    1. 寫操作前,檔案先格式化清空檔案

    2.清空操作,在執行open的w方法後,清空

print("寫的操作,寫檔案的時候,不能調用讀方法,讀檔案的時候,不能調用寫方法") f = open('python','w',encoding='utf8')f.write("I must learn python \nbecause, python is important \n")f.write("java is better?")f.write("maybe") #上面的語句,沒有加分行符號,所以輸出的內容是緊接的f.close()

運行結果:

開啟檔案後顯示如下

I must learn python because, python is important java is better?maybe

檔案的append方法

文法格式:

f = open('檔案名稱','a','encoding = utf8')

檔案這種方法為追加模式:1, 空白檔案中,直接從頭開始寫入內容; 2 有內容的檔案,會在末尾開始繼續寫入內容

樣本:

f = open('python','a',encoding='utf8')f.write("花開又花落")f.close()

運行結果:

I must learn python because, python is important java is better?maybe花開又花落 

readline 和 readlines

 readline是逐行讀取

readlines是全文讀取

樣本:

 print("readline方法") f = open('無題','r',encoding='utf8') a = f.readline() print("此時游標位置:",f.tell()) b = f.readline() print("此時游標位置:",f.tell()) print(a.strip()) #strip是字串方法中去除空格和換行的方法 print(b.strip())   print("readlines方法,會將每行的內容組成一個列表列印") f = open('無題','r',encoding='utf8') c = f.readlines() print(c) print(id(c)) print(id(f)) for i in c:  print(i.strip()) print("遍曆方法") f.seek(0) for i in f:  print(i.strip()) f.close() #檔案的操作中,close()方法一定不能忘記

運行結果:

readline方法此時游標位置: 23此時游標位置: 46昨夜星辰昨夜風畫樓西畔桂堂東readlines方法,會將每行的內容組成一個列表列印['昨夜星辰昨夜風\n', '畫樓西畔桂堂東\n', '身無彩鳳雙飛翼\n', '心有靈犀一點通']378268245344280昨夜星辰昨夜風畫樓西畔桂堂東身無彩鳳雙飛翼心有靈犀一點通遍曆方法昨夜星辰昨夜風畫樓西畔桂堂東身無彩鳳雙飛翼心有靈犀一點通

檔案的tell() 和 seek()方法

 樣本:

f = open('無題','r',encoding='utf8')f.read(4)print('當前游標位置',f.tell())f.seek(10)print('當前游標位置',f.tell())f.close()#read時,一個中文算三個字元

運行結果:

當前游標位置 12
當前游標位置 10

檔案操作之flush方法

import sys,timefor i in range(20): sys.stdout.write("#") sys.stdout.flush() time.sleep(1)

truncate方法

f = open('test','w')f.write("hello")f.write("\n")f.write("python")f.flush() #這樣不用執行close方法,記憶體中的資料,就會寫入到diskf.close()f = open('test','a')f.truncate(2) #截斷方法,游標從2開始往後截取f.close()

其他的檔案方法: r+ 讀寫方法

基於字元read & write

最基本的檔案操作當然就是在檔案中讀寫資料。這也是很容易掌握的。現在開啟一個檔案以進行寫操作:

fileHandle = open ( 'test.txt', 'w' )

‘w'是指檔案將被寫入資料,語句的其它部分很好理解。下一步就是將資料寫入檔案:

fileHandle.write ( 'This is a test.\nReally, it is.' )

這個語句將“This is a test.”寫入檔案的第一行,“Really, it is.”寫入檔案的第二行。最後,我們需要做清理工作,並且關閉檔案:

fileHandle.close()

正如你所見,在Python的物件導向機制下,這確實非常簡單。需要注意的是,當你再次使用“w”方式在檔案中寫資料,所有原來的內容都會被刪除。如果想保留原來的內容,可以使用“a”方式在檔案中結尾附加資料:

fileHandle = open ( 'test.txt', 'a' ) fileHandle.write ( '\n\nBottom line.' ) fileHandle.close()

然後,我們讀取test.txt,並將內容顯示出來:

fileHandle = open ( 'test.txt' ) print fileHandle.read() fileHandle.close()

以上語句將讀取整個檔案並顯示其中的資料。

基於行的讀寫 line

fileHandle = open ( 'test.txt' ) print fileHandle.readline() # "This is a test." fileHandle.close() 

同時,也可以將檔案內容儲存到一個list中:

fileHandle = open ( 'test.txt' ) fileList = fileHandle.readlines() for fileLine in fileList: print '>>', fileLine fileHandle.close() 

 或者在檔案中一次讀取幾個位元組的內容:

fileHandle = open ( 'test.txt' ) print fileHandle.read ( 1 ) # "T" fileHandle.seek ( 4 ) print FileHandle.read ( 1 ) # " "(原文有錯)

聯繫我們

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