天天看點

Python_檔案對象類型及其讀寫函數詳解軟體環境 file()檔案對象 open()檔案操作 最後

系統 

ubuntukylin 14.01

軟體 

python 2.7.3

ipython 4.0.0

file(name[, mode[, buffering]]) -> file object 

open a file. the mode can be ‘r’, ‘w’ or ‘a’ for reading (default),writing or appending. the file will be created if it doesn’t exist when opened for writing or appending; it will be truncated when opened for writing. add a ‘b’ to the mode for binary files. 

add a ‘+’ to the mode to allow simultaneous reading and writing. 

if the buffering argument is given, 0 means unbuffered, 1 means line buffered, and larger numbers specify the buffer size. the preferred way to open a file is with the builtin open() function. 

‘u’ cannot be combined with ‘w’ or ‘+’ mode. 

file()與open()的功能一緻,打開檔案或建立檔案。都屬于内建函數。 

file的屬性和方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

open(…) 

open(name[, mode[, buffering]]) -> file object 

open a file using the file() type, returns a file object. this is the 

preferred way to open a file. see file.__doc__ for further information. 

open()函數是file()函數的别名函數,能夠打開檔案并傳回一個檔案對象而非檔案的内容(應該了解為一個存儲着檔案的内容的對象,如果想擷取内容便需要對檔案對象進行操作)。可以指定不同的打開mode(rw),在調用open()函數後一定要調用檔案對象内建的close()函數來關閉檔案。一般結合try..finally語句來确定會關閉檔案對象。 

注意:當你open()一個檔案,實質上是将該檔案的内容加載到緩存中,是以當你open()檔案之後,對檔案做了修改也不會影響到open()傳回的對象的value。 

常用mode: 

1. r(read預設參數):已讀的方式打開檔案,不能調用write方法,當檔案不存在時報錯。 

2. w(write):已寫方式打開檔案,能夠寫入内容并覆寫,不能調用read方法,如果檔案不存在,則建立新同名檔案。 

3. a(append):已追加模式打開檔案,可以進行寫操作,如果恩健不存在,則建立同名檔案。 

4. +:使用+允許同時進行讀寫操作。 

5. u:支援所有類型的換行符(\n、\r、\r\n) 

6. b:表示對二進制檔案進行操作(圖檔、視訊)。 

7. t:對文本檔案進行操作。 

6種mode可以組合使用

以讀方式打開檔案後可以調用這三個函數read()\readline()\readlines() 

他們都可以傳遞一個int來指定需要讀取的總size(bytes)。 

注意:因為讀取的檔案會緩存到記憶體中,是以當需要讀取的檔案size大于記憶體時,需要指定每次讀入的size。

read(…) 

read([size]) -> read at most size bytes, returned as a string. 

if the size argument is negative or omitted, read until eof is reached. 

notice that when in non-blocking mode, less data than what was requested 

may be returned, even if no size parameter was given. 

讀取指定size的内容,預設參數為全部内容,傳回一個string類型對象。

readline(…) 

readline([size]) -> next line from the file, as a string. 

retain newline. a non-negative size argument limits the maximum 

number of bytes to return (an incomplete line may be returned then). 

return an empty string at eof. 

讀取檔案中的一行含有行結束符的内容,每執行一次會自動擷取往下一行的内容,傳回一個string。當讀取到最後一行再執行此函數時,會傳回一個空string,不會報錯。

一個綜合例子: 

open()+fileobject.readline()+try..finally+string.split()+os.path.exists() 

因為readline()函數傳回的是string類型對象,是以我們可以使用循環來周遊這一行中所有的元素。

在處理檔案資料中是非常常用的一個方法

readlines(…) 

readlines([size]) -> list of strings, each a line from the file. 

call readline() repeatedly and return a list of the lines so read. 

the optional size argument, if given, is an approximate bound on the 

total number of bytes in the lines returned. 

擷取檔案所有的内容,并傳回一個以每行内容作為一個string元素的list類型對象,本質是通過循環調用readline()實作的。

修改指定行的内容:

将檔案以r+的方式打開,并傳回一個對象。對對象的内容進行修改後,再将檔案以w+的方式打開,将對象的内容寫入到檔案中。實作對檔案指定行的内容修改。

read()和readlines()預設都是擷取檔案的所有内容。但是read()傳回一個string類型對象,元素是一個char。readlines()傳回一個list類型對象,元素是一個sting。而readline()擷取檔案的一行内容,傳回是一個string。

注意:調用write()、writeline()時,檔案原有的内容會被清空,因為檔案指針初始指向檔案的首行首個字母,而進行寫操作實質就是在檔案指針指向的位置開始寫入内容。

write(…) 

write(str) -> none. write string str to file. 

note that due to buffering, flush() or close() may be needed before 

the file on disk reflects the data written. 

将傳遞的string參數寫入并覆寫檔案内容,傳回none。需要執行close()或flush()後才會将記憶體的資料寫入到檔案中。 

注意:當你在沒有調用close()函數之前,你是可以調用多次write()函數來實作追加額效果,即後來的write()函數的寫入的内容并不會覆寫前一次使用write()函數寫入的内容,但是不會自動添加換行符。

結果:

open()+fileobject.write()+os.path.exists()+ergodicdictionary

writelines(…) 

writelines(sequence_of_strings) -> none. write the strings to the file. 

note that newlines are not added. the sequence can be any iterable object 

producing strings. this is equivalent to calling write() for each string. 

将傳遞的疊代對象的string元素逐個寫入檔案,相當于沒一行都調用額write()函數,但是不會自動添加換行符。 

修改上面的綜合例子:

系統标準輸入、輸出、err本質是一個類檔案對象。重定向即: 

<code>sys.stdout = fileobject_write</code> 

example:

檔案指針:當使用open()函數打開一個檔案并傳回一個檔案對象後,在檔案對象中會存放着目前”光标”在檔案中的位置,對檔案進行的讀、寫、截斷操作都是基于檔案指針,并從檔案指針+1開始進行的操作。。這個位置稱為檔案指針(從檔案頭部開始計算的位元組數),與c語言額指針概念相似,實質是檔案中位置的辨別。大部分的檔案操作都是基于檔案指針來實作的。

tell(…) 

tell() -&gt; current file position, an integer (may be a long integer).

truncate(…) 

truncate([size]) -&gt; none. truncate the file to at most size bytes. 

size defaults to the current file position, as returned by tell(). 

預設從檔案指針指向的位置開始截斷檔案内容,也可以通過傳遞int參數n來指定截斷的起始位置,即改變檔案指針的位置。從檔案指針指向的位置n開始,之後的檔案内容(不包含n)全部删除,以可修改mode打開的檔案可以使用此方法。

seek(…) 

seek(offset[, whence]) -&gt; none. move to new file position. 

可以接收偏移量和選項作為參數,傳回none。 

當whence==0時,将檔案指針從檔案頭部轉移到”偏移量”指定的字元處。 

當whence==1時,将檔案指針從檔案的目前位置往後轉移”偏移量”指定的字元數。 

當whence==2時,将檔案指針從檔案尾部向前移動”偏移量”指定的字元數。 

truncate()+tell()+seek()

總結:上面的例子可以看見,可以通過seek()函數來移動檔案指針,并結合truncate()來截斷檔案指針指定位置後面的檔案内容。同理,當傳遞int參數給truncate(n)後也會改變檔案指針。 

注意:當對檔案進行了讀、寫操作後都會改變檔案指針的值,而改變的值相當于操作過的len(string)。

最近一直在寫powershell,但是也沒有整理出比較子產品化的筆記,估計powershell主題還需要等待一段時間了。:(

jmilk

轉載:http://blog.csdn.net/jmilk/article/details/49982237