python文件讀寫
python 進行文件讀寫的內建函數是open或file
file_hander(文件句柄或者叫做對象)= open(filename,mode)
mode:
模式 說明
r 只讀
r+ 讀寫
w 寫入,先刪除源文件,在重新寫入,如果文件沒有則創建
w+ 讀寫,先刪除源文件,在重新寫入,如果文件沒有則創建(可以寫入寫出)
讀文件:
>>> fo = open("/root/a.txt")>>> fo
<open file '/root/a.txt', mode 'r' at 0x7f5095dec4e0>
>>> fo.read()
'hello davehe/ni am emily/nemily emily/n'
>>> fo.close()>>> fo.read() #對象已關閉,在讀取就讀不到
Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: I/O operation on closed file
>>> f1 = file("/root/a.txt") >>> f1.read()
'hello davehe/ni am emily/nemily emily/n'
>>> f1.close()
寫文件:
root@10.1.6.200:~# ls -l new.txt
ls: cannot access new.txt: No such file or directory
>>> fnew = open("/root/new.txt",'w') w參數文件沒有則創建>>> fnew.write('hello /n i am dave')
這時查看文件數據其實還只是在緩存區中,沒有真正落到文件上.
root@10.1.6.200:~# cat new.txt root@10.1.6.200:~#
只要我把文件關閉,數據會從緩存區寫到文件里
>>> fnew.close()root@10.1.6.200:~# cat new.txt
hello i am dave
再次使用w參數,文件會被清空,所以用該參數需要謹慎.
>>> fnew = open("/root/new.txt","w")
root@10.1.6.200:~# cat new.txt root@10.1.6.200:~#
mode使用r+參數:
>>> fnew = open("/root/new.txt",'r+')>>> fnew.read()
'hello dave'
>>> fnew.write('i am dave')>>> fnew.close()
root@10.1.6.200:~# cat new.txt
hello davei am dave
這次打開文件,直接寫入,會發現ooo替換開頭字母,因為上面讀取操作使用了指針在寫就寫在后面.而這次是直接從頭寫入.
>>> fnew = open("/root/new.txt",'r+')>>> fnew.write('ooo')>>> fnew.close()
root@10.1.6.200:~# cat new.txt
ooolo davei am dave
文件對象方法
下面文件對象方法
- FileObject.close()
- String=FileObject.readline([size])
- List = FileObject.readlines([size])
- String = FileObject.read([size]) read:讀取所有數據
- FileObject.next()
- FileObject.write(string)
- FileObject.writelines(List)
- FlieObject.seek(偏移量,選項)
- FlieObject.flush() 提交更新
>>> for i in open("/root/a.txt"): 用open可以返回迭代類型的變量,可以逐行讀取數據... print i...
hello davehei am emilyemily emily
FileObject.readline: 每次讀取文件的一行,size是指每行每次讀取size個字節,直到行的末尾,超出范圍會讀取空字符串
>>> f1 = open("/root/a.txt")>>> f1.readline()
'hello davehe/n'
>>> f1.readline()
'i am emily/n'
>>> f1.readline()
'emily emily/n'
>>> f1.readline()''>>> f1.readline()''>>>f1.close()
FileObject.readlines:返回一個列表
>>> f1 = open("/root/a.txt")>>> f1.readlines()
['hello davehe/n', 'i am emily/n', 'emily emily/n']''
FileObject.next:返回當前行,并將文件指針到下一行,超出范圍會給予警示,停止迭代.
>>> f1 = open("/root/a.txt")>>> f1.next()
'hello davehe/n'
>>> f1.next()
'i am emily/n'
>>> f1.next()
'emily emily/n'
>>> f1.next()
Traceback (most recent call last): File "<stdin>", line 1, in <module>StopIteration
FileObject.write:write和后面writelines在寫入前會是否清除文件中原來所有的數據,在重新寫入新的內容,取決于打開文件的模式.
FileObject.writelines(List):多行寫,效率比write高,速度更快,少量寫入可以使用write
>>> l = ["python/n","python/n","python/n"]>>> f1 = open('/root/a.txt','a')>>> f1.writelines(l)>>> f1.close()
root@10.1.6.200:~# cat a.txt
hello davehei am emilyemily emilypythonpythonpython
FlieObject.seek(偏移量,選項):可以在文件中移動文件指針到不同的位置.
位置的默認值為0,代表從文件開頭算起(即絕對偏移量),1代表從當前位置算起,2代表從文件末尾算起.
>>> f1 = open('/root/a.txt','r+')>>> f1.read()
'hello davehe/ni am emily/nemily emily/npython/npython/npython/n'
>>> f1.seek(0,0) 指針指到開頭,在讀>>> f1.read()
'hello davehe/ni am emily/nemily emily/npython/npython/npython/n'
>>> f1.read()''>>> f1.seek(0,0)>>> f1.seek(0,2) 指針指到末尾,在讀>>> f1.read()''
下面看個小實例,查找a.txt中emily出現幾次
root@10.1.6.200:~# vim file.py
#!/usr/bin/env pythonimport ref1 = open('/root/a.txt')count = 0for s in f1.readlines(): li = re.findall("emily",s) if len(li) > 0: count = count + len(li)print "this is have %d emily" % count f1.close()
root@10.1.6.200:~# cat a.txt
hello davehei am emilyemily emily
root@10.1.6.200:~# python file.py
this is have 3 emily