python文件读写
python 进行文件读写的内建函数是open或file
file_hander(文件句柄或者叫做对象)= open(filename,mode)
mode:
模式 说明
r 只读
r+ 读写
w 写入,先删除源文件,在重新写入,如果文件没有则创建
w+ 读写,先删除源文件,在重新写入,如果文件没有则创建(可以写入写出)
读文件:
1
2
|
>>> fo = open ( "/root/a.txt" ) >>> fo |
1
|
<open file '/root/a.txt', mode 'r' at 0x7f5095dec4e0> |
1
|
>>> fo.read() |
1
|
'hello davehe\ni am emily\nemily emily\n' |
1
2
|
>>> fo.close() >>> fo.read() #对象已关闭,在读取就读不到 |
1
2
3
|
Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: I/O operation on closed file |
1
2
|
>>> f1 = file ( "/root/a.txt" ) >>> f1.read() |
1
|
'hello davehe\ni am emily\nemily emily\n' |
1
|
>>> f1.close() |
写文件:
1
|
root@10.1.6.200:~ # ls -l new.txt |
1
|
ls: cannot access new.txt: No such file or directory |
1
2
|
>>> fnew = open ( "/root/new.txt" , 'w' ) w参数文件没有则创建 >>> fnew.write( 'hello \n i am dave' ) |
这时查看文件数据其实还只是在缓存区中,没有真正落到文件上.
1
2
|
root@10.1.6.200:~ # cat new.txt root@10.1.6.200:~ # |
只要我把文件关闭,数据会从缓存区写到文件里
1
2
|
>>> fnew.close() root@10.1.6.200:~ # cat new.txt |
1
2
|
hello i am dave |
再次使用w参数,文件会被清空,所以用该参数需要谨慎.
1
|
>>> fnew = open ( "/root/new.txt" , "w" ) |
1
2
|
root@10.1.6.200:~ # cat new.txt root@10.1.6.200:~ # |
mode使用r+参数:
1
2
|
>>> fnew = open ( "/root/new.txt" , 'r+' ) >>> fnew.read() |
1
|
'hello dave' |
1
2
|
>>> fnew.write( 'i am dave' ) >>> fnew.close() |
1
|
root@10.1.6.200:~ # cat new.txt |
1
|
hello davei am dave |
这次打开文件,直接写入,会发现ooo替换开头字母,因为上面读取操作使用了指针在写就写在后面.而这次是直接从头写入.
1
2
3
|
>>> fnew = open ( "/root/new.txt" , 'r+' ) >>> fnew.write( 'ooo' ) >>> fnew.close() |
1
|
root@10.1.6.200:~ # cat new.txt |
1
|
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() 提交更新
1
2
3
|
>>> for i in open ( "/root/a.txt" ): 用 open 可以返回迭代类型的变量,可以逐行读取数据 ... print i ... |
1
2
3
|
hello davehe i am emily emily emily |
FileObject.readline: 每次读取文件的一行,size是指每行每次读取size个字节,直到行的末尾,超出范围会读取空字符串
1
2
|
>>> f1 = open ( "/root/a.txt" ) >>> f1.readline() |
1
|
'hello davehe\n' |
1
|
>>> f1.readline() |
1
|
'i am emily\n' |
1
|
>>> f1.readline() |
1
|
'emily emily\n' |
1
2
3
4
5
|
>>> f1.readline() '' >>> f1.readline() '' >>>f1.close() |
FileObject.readlines:返回一个列表
1
2
|
>>> f1 = open ( "/root/a.txt" ) >>> f1.readlines() |
1
|
['hello davehe\n', 'i am emily\n', 'emily emily\n']'' |
FileObject.next:返回当前行,并将文件指针到下一行,超出范围会给予警示,停止迭代.
1
2
|
>>> f1 = open ( "/root/a.txt" ) >>> f1. next () |
1
|
'hello davehe\n' |
1
|
>>> f1. next () |
1
|
'i am emily\n' |
1
|
>>> f1. next () |
1
|
'emily emily\n' |
1
|
>>> f1. next () |
1
2
3
|
Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration |
FileObject.write:write和后面writelines在写入前会是否清除文件中原来所有的数据,在重新写入新的内容,取决于打开文件的模式.
FileObject.writelines(List):多行写,效率比write高,速度更快,少量写入可以使用write
1
2
3
4
|
>>> l = [ "python\n" , "python\n" , "python\n" ] >>> f1 = open ( '/root/a.txt' , 'a' ) >>> f1.writelines(l) >>> f1.close() |
1
|
root@10.1.6.200:~ # cat a.txt |
1
2
3
4
5
6
|
hello davehe i am emily emily emily python python python |
FlieObject.seek(偏移量,选项):可以在文件中移动文件指针到不同的位置.
位置的默认值为0,代表从文件开头算起(即绝对偏移量),1代表从当前位置算起,2代表从文件末尾算起.
1
2
|
>>> f1 = open ( '/root/a.txt' , 'r+' ) >>> f1.read() |
1
|
'hello davehe\ni am emily\nemily emily\npython\npython\npython\n' |
1
2
|
>>> f1.seek( 0 , 0 ) 指针指到开头,在读 >>> f1.read() |
1
|
'hello davehe\ni am emily\nemily emily\npython\npython\npython\n' |
1
2
3
4
5
6
|
>>> f1.read() '' >>> f1.seek( 0 , 0 ) >>> f1.seek( 0 , 2 ) 指针指到末尾,在读 >>> f1.read() '' |
下面看个小实例,查找a.txt中emily出现几次
1
|
root@10.1.6.200:~ # vim file.py |
1
2
3
4
5
6
7
8
9
10
|
#!/usr/bin/env python import re f1 = open ( '/root/a.txt' ) count = 0 for 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() |
1
|
root@10.1.6.200:~ # cat a.txt |
1
2
3
|
hello davehe i am emily emily emily |
1
|
root@10.1.6.200:~ # python file.py |
1
|
this is have 3 emily |