欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python的文件操作方法匯總

 更新時(shí)間:2017年11月10日 08:50:07   作者:不進(jìn)_則退  
文件操作對(duì)編程語言的重要性不用多說,如果數(shù)據(jù)不能持久保存,信息技術(shù)也就失去了意義。按照本人經(jīng)驗(yàn),IO也是蠻頭疼的一件事,因?yàn)椴粫?huì)用得太多,所以總是記不住API,每次都要重新google就會(huì)打斷思路,還不一定每次都快速得到正確的文章。

文件的讀操作

示例:

 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中,文件中一個(gè)中英文都占位1

運(yùn)行結(jié)果:

復(fù)制代碼

->文件句柄的獲取,讀操作:
昨夜星辰昨夜風(fēng)
畫樓西畔桂堂東
身無彩鳳雙飛翼
心有靈犀一點(diǎn)通
->例二:
昨夜星辰昨夜風(fēng)
畫

文件的寫操作

知識(shí)點(diǎn):

    1. 寫操作前,文件先格式化清空文件

    2.清空操作,在執(zhí)行open的w方法后,清空

print("寫的操作,寫文件的時(shí)候,不能調(diào)用讀方法,讀文件的時(shí)候,不能調(diào)用寫方法")
 
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") #上面的語句,沒有加換行符,所以輸出的內(nèi)容是緊接的
f.close()

運(yùn)行結(jié)果:

打開文件后顯示如下

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

文件的append方法

語法格式:

f = open('文件名','a','encoding = utf8')

文件這種方法為追加模式:1, 空白文件中,直接從頭開始寫入內(nèi)容; 2 有內(nèi)容的文件,會(huì)在末尾開始繼續(xù)寫入內(nèi)容

示例:

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

運(yùn)行結(jié)果:

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("此時(shí)光標(biāo)位置:",f.tell())
 b = f.readline()
 print("此時(shí)光標(biāo)位置:",f.tell())
 print(a.strip()) #strip是字符串方法中去除空格和換行的方法
 print(b.strip())
 
 
 print("readlines方法,會(huì)將每行的內(nèi)容組成一個(gè)列表打印")
 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()方法一定不能忘記

運(yùn)行結(jié)果:

readline方法
此時(shí)光標(biāo)位置: 23
此時(shí)光標(biāo)位置: 46
昨夜星辰昨夜風(fēng)
畫樓西畔桂堂東
readlines方法,會(huì)將每行的內(nèi)容組成一個(gè)列表打印
['昨夜星辰昨夜風(fēng)\n', '畫樓西畔桂堂東\n', '身無彩鳳雙飛翼\n', '心有靈犀一點(diǎn)通']
37826824
5344280
昨夜星辰昨夜風(fēng)
畫樓西畔桂堂東
身無彩鳳雙飛翼
心有靈犀一點(diǎn)通
遍歷方法
昨夜星辰昨夜風(fēng)
畫樓西畔桂堂東
身無彩鳳雙飛翼
心有靈犀一點(diǎn)通

文件的tell() 和 seek()方法

 示例:

f = open('無題','r',encoding='utf8')
f.read(4)
print('當(dāng)前光標(biāo)位置',f.tell())

f.seek(10)
print('當(dāng)前光標(biāo)位置',f.tell())
f.close()

#read時(shí),一個(gè)中文算三個(gè)字符

運(yùn)行結(jié)果:

當(dāng)前光標(biāo)位置 12
當(dāng)前光標(biāo)位置 10

文件操作之flush方法

import sys,time

for 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() #這樣不用執(zhí)行close方法,內(nèi)存中的數(shù)據(jù),就會(huì)寫入到disk
f.close()

f = open('test','a')
f.truncate(2) #截?cái)喾椒?光標(biāo)從2開始往后截取
f.close()

其他的文件方法: r+ 讀寫方法

基于字符read & write

最基本的文件操作當(dāng)然就是在文件中讀寫數(shù)據(jù)。這也是很容易掌握的?,F(xiàn)在打開一個(gè)文件以進(jìn)行寫操作:

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

‘w'是指文件將被寫入數(shù)據(jù),語句的其它部分很好理解。下一步就是將數(shù)據(jù)寫入文件:

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

這個(gè)語句將“This is a test.”寫入文件的第一行,“Really, it is.”寫入文件的第二行。最后,我們需要做清理工作,并且關(guān)閉文件:

fileHandle.close()

正如你所見,在Python的面向?qū)ο髾C(jī)制下,這確實(shí)非常簡(jiǎn)單。需要注意的是,當(dāng)你再次使用“w”方式在文件中寫數(shù)據(jù),所有原來的內(nèi)容都會(huì)被刪除。如果想保留原來的內(nèi)容,可以使用“a”方式在文件中結(jié)尾附加數(shù)據(jù):

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

然后,我們讀取test.txt,并將內(nèi)容顯示出來:

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

以上語句將讀取整個(gè)文件并顯示其中的數(shù)據(jù)。

基于行的讀寫 line

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

同時(shí),也可以將文件內(nèi)容保存到一個(gè)list中:

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

 或者在文件中一次讀取幾個(gè)字節(jié)的內(nèi)容:

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

相關(guān)文章

最新評(píng)論