python程序中經常用到的讀文件:
f = open("___", 'r')
for line in f:#這里每次讀取文件的一行,line為字符串,串尾包括了'\n'!!!
print line
f.close()
轉自:http://www.jb51.net/article/58002.htm
1. open()語法
open(file[, mode[, buffering[, encoding[, errors[, newline[, closefd=True]]]]]])
open函數有很多的參數,常用的是file,mode和encoding
file文件位置,需要加引號
mode文件打開模式,見下面3
buffering的可取值有0,1,>1三個,0代表buffer關閉(只適用於二進制模式),1代表line buffer(只適用於文本模式),>1表示初始化的buffer大小;
encoding表示的是返回的數據采用何種編碼,一般采用utf8或者gbk;
errors的取值一般有strict,ignore,當取strict的時候,字符編碼出現問題的時候,會報錯,當取ignore的時候,編碼出現問題,程序會忽略而過,繼續執行下面的程序。
newline可以取的值有None, \n, \r, ”, ‘\r\n',用於區分換行符,但是這個參數只對文本模式有效;
closefd的取值,是與傳入的文件參數有關,默認情況下為True,傳入的file參數為文件的文件名,取值為False的時候,file只能是文件描述符,什么是文件描述符,就是一個非負整數,在Unix內核的系統中,打開一個文件,便會返回一個文件描述符。
2. Python中file()與open()區別
兩者都能夠打開文件,對文件進行操作,也具有相似的用法和參數,但是,這兩種文件打開方式有本質的區別,file為文件類,用file()來打開文件,相當於這是在構造文件類,而用open()打開文件,是用python的內建函數來操作,建議使用open
3. 參數mode的基本取值
Character | Meaning |
‘r' | open for reading (default) |
‘w' | open for writing, truncating the file first |
‘a' | open for writing, appending to the end of the file if it exists |
‘b' | binary mode |
‘t' | text mode (default) |
‘+' | open a disk file for updating (reading and writing) |
‘U' | universal newline mode (for backwards compatibility; should not be used in new code) |
r、w、a為打開文件的基本模式,對應着只讀、只寫、追加模式;
b、t、+、U這四個字符,與以上的文件打開模式組合使用,二進制模式,文本模式,讀寫模式、通用換行符,根據實際情況組合使用、
常見的mode取值組合
1
2
3
4
5
6
7
8
9
10
11
|
r或rt 默認模式,文本模式讀
rb 二進制文件
w或wt 文本模式寫,打開前文件存儲被清空
wb 二進制寫,文件存儲同樣被清空
a 追加模式,只能寫在文件末尾
a + 可讀寫模式,寫只能寫在文件末尾
w + 可讀寫,與a + 的區別是要清空文件內容
r + 可讀寫,與a + 的區別是可以寫到文件任何位置
|
4. 測試
測試文件test.txt,內容如下:
1
2
3
|
Hello,Python
www.jb51.net
This is a test file
|
用一小段代碼來測試寫入文件直觀的顯示它們的不同
test = [ "test1\n", "test2\n", "test3\n" ]
f = open("test.txt", "a+")
try:
#f.seek(0)
for l in test:
f.write(l)
finally:
f.close()
a+、w+和r+模式的區別(測試后還原test.txt)
a+模式
1
2
3
4
5
6
7
|
# cat test.txt
Hello, Python
www.jb51.net
This is a test file
test1
test2
test3
|
w+模式
1
2
3
4
|
# cat test.txt
test1
test2
test3
|
r+模式
在寫入文件前,我們在上面那段代碼中加上一句f.seek(0),用來定位寫入文件寫入位置(文件開頭),直接覆蓋字符數(注意\n也是一個字符)
1
2
3
4
5
6
|
# cat test.txt
test1
test2
test3
inuxeye.com
This is a test file
|
注意:r+模式打開文件時,此文件必須存在,否則就會報錯,‘r'模式也如此
其他測試
>>> f = open('test.txt')
>>> f.read() #讀取整個文件,字符串顯示
'Hello,Python\nwww.jb51.net\nThis is a test file\n'
>>> f.read() #指針在文件末尾,不能再讀取內容
''
>>> f = open('test.txt')
>>> f.readline() #一次讀一行,指針在該行末尾
'Hello,Python\n'
>>> f.tell() #改行的字符長度
13
>>> f.readline()
'www.jb51.net\n'
>>> f.tell()
30
>>> f.readline()
'This is a test file\n'
>>> f.tell()
50
>>> f.readline()
''
>>> f.tell() #指針停在最后一行
50
>>> f = open('test.txt')
>>> f.readlines() #讀取整個文件,以列表顯示
['Hello,Python\n', 'www.jb51.net\n', 'This is a test file\n']
>>> f.tell() #指針在最后一行
50
1
2
3
4
5
6
7
8
|
>>> f = open ( 'test.txt' , 'w' ) #覆蓋創建新文件
>>> f.write( 'Hello,Python!' ) #如果寫入內容小於1024,會存在內存,否則需要刷新
>>> f.flush() #寫入到硬盤
>>> f.close() #關閉文件會自動刷新
>>> f.write( 'Hello,Linuxeye' ) #關閉后,寫失敗,提示文件已經關閉
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
ValueError: I / O operation on closed file
|