<<Python基礎教程>>學習筆記 | 第11章 | 檔案和素材,
開啟檔案
open(name[mode[,buffing])
name: 是強制選項,模式和緩衝是可選的
#如果檔案不在,會報下面錯誤:
>>> f = open(r'D:\text.txt','r')Traceback (most recent call last): File "<stdin>", line 1, in <module>IOError: [Errno 2] No such file or directory: 'D:\\text.txt'
檔案模式
NOTE:
1. 預設的方式,比如說open('filename')是讀模式
2. r+, 則表示可讀寫
3. 如果是二進位檔案或圖形檔案,則必須用緩衝模式
4. 普通的w模式會覆蓋檔案的內容,a模式則不會.
5. rb則可以用來讀取二進位檔案.
6, 通過參數模式中使用U參數,能夠在開啟檔案時使用通用的分行符號支援模式,無論\r,\n\r,都會換成\n,而不用考慮啟動並執行平台.
緩衝:
第三個參數,可選
0或者False: 無緩衝,所有操作直接針對硬碟
1或者True: 有緩衝,記憶體代替硬碟,速度快,只有close,flush才寫入硬碟同步.
> 1 : 表示緩衝區的大小
-1 : 表示預設的緩衝區大小
基本檔案方法
NOTE: 類檔案對象是支援一些檔案的方法的對象,比如file方法,最重要的兩個方法,read,write方法.還有urllib.urlopen返回對象,他們支援的方法有: read,readline,readlines
三種標準的流
sys.stdin :標準輸入資料流,可通過輸入或者使用管道把它和其它程式的標準輸出連結起來提供文本
sys.stdout :放置input,raw_input寫入的資料,可在螢幕上顯示,也可通過|串連到其它程式的標準輸入
sys.stderr :錯誤資訊,比如說棧追蹤被寫入sys.stderr.
讀和寫
最重要的能力是提供讀寫
#對空檔案來說: 提供寫時,會在已在字串末尾追加,
>>> f = open('somefile.txt','w')>>> f.write('Hello,')>>> f.write('World!')>>> f.close()#somefile.txt檔案內容Hello,World!#對於非空檔案:提供w方法時,會覆蓋檔案中的內容
>>> f = open('somefile','w')>>> f.write('This is 1st line.\n')>>> f.write('This is 2nd line.')>>> f.close()#somefile.txt檔案內容This is 1st line.This is 2nd line.簡單讀取的例子:
>>> f = open('somefile.txt','r')>>> f.read(16)#先讀取16個字元'This is 1st line'>>> f.read() #會讀取剩下的內容,除非seek定位到0,重新讀取'.\nThis is 2nd line.'>>> f.close()管道輸出
$ cat somefile.txt | python somescript.py | sort
一個簡單例子: 統計一個文本中單詞的數量
$ cat somefile.txt
This is a book!
That is a dog!
Who are you?
指令碼清單
#somescript.pyimport systext = sys.stdin.read() words = text.split()print "Word Count:", len(words)
輸出結果:
# cat somefile.txt | python somescript.pyWord Count: 11
隨機訪問:
用seek和tell來訪問自己感興趣的部分
seek(offset[,whence]). offset,位移量,Whence值
0: 開始位置
1: 當前位置
2: 檔案末尾
簡單例子:
>>> f = open('somefile.txt','w')>>> f.write('01234567890123456789')>>> f.seek(5)>>> f.write('Hello,World!')>>> f.close()>>> f = open('somefile.txt')>>> f.read()'01234Hello,World!789'#用tell來返回當前檔案的位置>>> f = open('somefile.txt')>>> f.read(3)'012'>>> f.read(2)'34'>>> f.tell()5L讀寫行:
readline : 讀取行,包括分行符號
readlines: 讀取所有行
write: 寫一行, 注意:沒有writeline方法
writelines: 寫多行
NOTE: 如何判斷不同的行以什麼結尾? os.linesep
#UNIX >>> import os>>> os.linesep'\n'#WINDOWS>>> import os>>> os.linesep'\r\n'
關閉檔案
時刻記得close()來關閉檔案,這樣做的目的:
1. 出於安全考慮,防止檔案因為某些原因崩潰,寫不進資料
2. 出於資料同步考慮,close(),才會往硬碟中寫資料
3. 出於效率的考慮,記憶體中的資料可清空一部分出來
為了確保程式結束時close(),可以用try/finally結合使用
# Open your file heretry: # Write data to your filefinally: file.close()
NOTE: 一般檔案在close()之後才會寫入硬碟,如果想不執行close()方法,又可以看到寫入的內容,那麼flush就派上用場了.
使用基本方法:
#測試文本somefile.txt
Welcome to this file
There is nothing here except
This stupid haiku
首先讀取指定字元
>>> f = open(r'd:\Learn\Python\somefile.txt')>>> f.read(7)'Welcome'>>> f.read(4)' to '>>> f.close()
其次讀取所有的行
>>> f = open(r'd:\Learn\Python\somefile.txt','r')>>> print f.read()Welcome to this fileThere is nothing here exceptThis stupid haiku
接著是讀取行
>>> f.close()>>> f = open(r'd:\Learn\Python\somefile.txt')>>> for i in range(3):... print str(i) + ':' + f.readline()...0:Welcome to this file1:There is nothing here except2:This stupid haiku
再讀取所有行:
>>> import pprint>>> pprint.pprint(open('somefile.txt').readlines())['Welcome to this file\n', 'There is nothing here except\n', 'This stupid haiku']下面是寫檔案
>>> f = open(r'somefile.txt','w')>>> f.write('this\nis no\nhaiku')>>> f.close()運行檔案後,內容如下:thisis nohaiku最後是writelines
>>> f = open(r'somefile.txt')>>> lines = f.readlines()>>> f.close()>>> lines[1] = "isn't a\n">>> f = open('somefile.txt','w')>>> f.writelines(lines)>>> f.close()運行後,檔案內容如下:this isn't ahaiku對檔案內容進行迭代
基本的方法如: read,readline,readlines,還有xreadline和檔案迭代器
下面的例子都使用的虛擬函數process(),表示每個字元或每行處理過程
def process(string):
print 'Processing', string
更有用的實現是:在資料結構中儲存資料,計算和值。用re模組來替代模式或者增加行號.如果要實現上面的功能,
則應該講filename變數設定為實際的檔案名稱.
按位元組處理
def process(string): print 'Processing...', stringf = open('somefile.txt')char = f.read(1)while char: process(char) char = f.read(1)f.close()代碼重用通常是件壞事,懶惰是美德。重寫下代碼如下:
def process(string): print 'Processing...', stringf = open('somefile.txt')while True: char = f.read(1) if not char: break process(char)f.close()NOTE: 這樣寫就比上面要好,避免了重複的代碼.
按行操作
f = open(filename)while True: line = f.readline() if not line: break process(line)f.close()
讀取所有內容
如果檔案不是很大,可以用read(),或者readlines()讀取的內容作為字串來處理.
#用read來迭代每個字串
f = open(r'D:\Work\Python\somefile.txt')for char in f.read(): process(char)f.close()
#用readlines來迭代行
f = open(r'D:\Work\Python\somefile.txt','r')for line in f.readlines(): process(line)f.close()
使用fileinput實現懶惰行迭代
在需要對一個大檔案進行迭代時,readlines會佔用太多的記憶體。這個時候可以使用while迴圈和readline方法來替代。
import fileinputdef process(string): print 'Processing...', stringfor line in fileinput.input('somefile.txt'): process(line)檔案迭代器
#Python中檔案是可以迭代的,寫起來也很優雅
f = open('somefile.txt')for line in f: print line,f.close()#如果希望Python來完成關閉的動作,對檔案進行迭代,而不使用變數儲存變數
#代碼可以更加精簡
for line in open('somefile.txt'): print line,sys.stdin也是可以迭代的,簡單代碼如下:
import sysfor line in sys.stdin: print line,運行結果:D:\Work\Python>python file.py#輸入下面兩行Hello,World!Hello,Jerry!^Z #按下CTRL+Z鍵後,輸入的內容,顯示Hello,World!Hello,Jerry!
#可以對檔案迭代器執行和普通迭代器相同的操作。比如將它們轉換為字串列表,這樣所達到的效果和使用readlines一樣.如下例:
>>> f = open('somefile.txt','w')>>> f.write('First line\n')>>> f.write('Second line\n')>>> f.write('Third line\n')>>> f.close()>>> lines = list(open('somefile.txt'))>>> lines['First line\n', 'Second line\n', 'Third line\n']>>> first,second,third = open('somefile.txt')>>> first'First line\n'>>> second'Second line\n'>>> third'Third line\n'NOTE:
1.用序列來做解包操作非常使用
2.讀檔案操作時,可以不用close()
------
本章新函數
file(name[,mode[,buffering]]) 開啟一個檔案並返回一個檔案對象
open(name[,mode[,buffering]]) file的別名;在開啟檔案,使用open而不是file