同樣是《python基礎(chǔ)教程(第二版)》的內(nèi)容,只是后面內(nèi)容學(xué)起來,相比前面會(huì)比較有趣,也更加實(shí)用,所以,將“基礎(chǔ)”改為“進(jìn)階”。 python 電子書分享地址:http:///Q2U87uGrNiTA3
本節(jié)講文件的操作 -------------------------------
打開文件
open函數(shù)用來打開文件,語法如下: open(name[, mode[,buffering]]) open函數(shù)使用一個(gè)文件名作為唯一的強(qiáng)制參數(shù),然后返回一個(gè)文件對(duì)象。假設(shè)我要打開我硬盤(I:/python/test.txt) 文件,可以用下面方法: >>> f = open(r'i:\python\test.txt')
open函數(shù)中模式參數(shù)的常用值
基本文件方法
打開文件是第一步,下面就需要對(duì)文件進(jìn)行讀或?qū)?,可?span style="font-family: 'Times New Roman';">write 和 read方法進(jìn)行讀或?qū)憽?/span> #寫入文件內(nèi)容 >>> f = open('test.txt','w') >>> f.write('hello,') >>> f.write('world!') >>> f.close() # 讀取文件內(nèi)容 >>> f = open('test.txt','r') >>> f.read(4) #讀取前4個(gè)字符 'hell' >>> f.read() #讀取剩余的所有字符 'o,world!'
關(guān)閉文件 應(yīng)該牢記使用close方法關(guān)閉文件。盡管一個(gè)文件對(duì)象在退出程序后會(huì)自動(dòng)關(guān)閉,但關(guān)閉文件是沒什么害處的,可以避免在某些操作系統(tǒng)或設(shè)置中進(jìn)行無用的修改,這樣做也會(huì)避免用完系統(tǒng)中打開文件的配額。
使用基本文件方法
假如test.txt文件包含如下內(nèi)容: ----------------------------- Welcome to this file There is nothing here except This stupid haiku ----------------------------- 下面是基本讀文件的方法: # read(n) 指定參數(shù) >>> f = open(r'I:\python\test.txt') >>> f.read(7) 'Welcome' >>> f.read(4) ' to ' >>> f.close() # read() 不指定參數(shù) >>> f = open(r'I:\python\test.txt') >>> print f.read() Welcome to this file There is nothing here except This stupid haiku >>> f.close() # readline() >>> f = open(r'I:\python\test.txt') >>> for i in range(3): print str(i) + ':' + f.readline() 0:Welcome to this file 1:There is nothing here except 2:This stupid haiku >>> f.close() #readlines() >>> import pprint >>> pprint.pprint(open(r'I:\python\test.txt').readlines()) ['Welcome to this file\n', 'There is nothing here except\n', 'This stupid haiku'] readline返回一行的字符串, readlines返回包含文件所有內(nèi)容的字符串列表, 每個(gè)元素是一行的字符串。 pprint 模塊的pprint方法將內(nèi)容分成每個(gè)小項(xiàng)單行顯示。
下面是寫文件的基本方法: >>> f = open(r'I:\python\test.txt','w') #默認(rèn)是讀文件,可以不加‘r’,寫文件一定要加’w’ >>> f.write('this\nis no \nhaiku') >>> f.close()
>>> f = open(r'I:\python\test.txt') >>> lines = f.readlines() >>> lines[1] = "isn't a\n" >>> f = open(r'I:\python\test.txt','w') >>> f.writelines(lines) >>> f.close()
對(duì)文件內(nèi)容進(jìn)行迭代
1、接字節(jié)處理 最常見的對(duì)文件內(nèi)容進(jìn)行迭代的方法是while循環(huán)中使用read方法。例如,對(duì)每個(gè)字符進(jìn)行循環(huán),可以用下面方法實(shí)現(xiàn): read方法返回的字符串會(huì)包含一個(gè)字符,直到文件末尾,read返回一個(gè)空的字符串,char將變?yōu)榧佟?/span> 可以看到,char = f.read(1) 被重復(fù)地使用,代碼重復(fù)通過被認(rèn)為是一件壞事,看看下面的方法: 這里break語句被頻繁的使用(這樣會(huì)讓代碼比較難懂),盡管如此,但它仍然要比前面的方法好。
2、讀取所有內(nèi)容 如果文件不是很大,那么可以使用不帶參數(shù)的read方法一次讀取整個(gè)文件,或者使用readlines方法。 #用read迭代每個(gè)字符 f = open(filename) for char in f.read(): process(char) f.close()
3、用fileinput 來進(jìn)行迭代 fileinput模塊包含了打開文件的函數(shù),,只需要傳一個(gè)文件名給它 import fileinput for line in fileinput.input(filename): process(line)
4、文件迭代器 好吧!這是python2.2之后才有的方法,如果它一開始就有,上面的方法也許就不存在了。文件對(duì)象是可以迭代的,這就意味著可以直接在for循環(huán)中對(duì)他們進(jìn)行迭代 f = open(filename) for line in f: process(line) f.close()
再來看下面例子: >>> f = open(r'I:\python\test.txt','w') >>> f.write('First line\n') >>> f.write('Second line\n') >>> f.write('Third line\n') >>> f.close() >>> lines = list(open(r'I:\python\test.txt')) >>> lines ['First line\n', 'Second line\n', 'Third line\n'] >>> first,second,third = open(r'I:\python\test.txt') >>> first 'First line\n' >>> second 'Second line\n' >>> third 'Third line\n' 在這個(gè)例子中:
|
|