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