python对文件的操作

创建与打开

对文件的读写操作

对文件的复制,删除,重命名等操作

举例说明

python打开一个文件可以用open或file

>>> fo=open('./12.txt')

>>> fo.read()

'abc\nthis is \nthis is add\nthisddd is add\nthisdddddddffggd is add\n'

>>> fo

<open file './12.txt', mode 'r' at 0xb77a5e90>

>>> fo.close()

向文件中写入数据的模式

r 只读

r+ 读写

w 写入,先删除原来的文件,在重新写入,如果文件没有则创建文件

w+ 读写,先删除原来的文件,在重新写入,如果没有则创建(可以写入输出)

a 写入,在文件末尾追加新的内容,文件不存在,则创建

a+ 读写,在文件末尾追加新的内容,文件不存在,则创建

b 打开二进制的文件,可以与r,w,a,+ 结合使用

U 支持所有的换行符。


例子

>>> fnew=open('./11.txt','w')

>>> fnew.write('hello,world.\nthis is a test file')

>>> fnew=open('./11.txt')

>>> fnew.read()

'hello,world.\nthis is a test file'

>>> fnew.close()

>>> fnew=open('./11.txt')

>>> fnew.close()

>>> fnew=open('./11.txt')    

>>> fnew.read()

''

r+读写

>>> fnew=open('./12.txt','r+')

>>> fnew.read()

'abc\nthis is \nthis is add\nthisddd is add\nthisdddddddffggd is add\n'

>>> fnew.write("harry is a boy")

>>> fnew.close()

>>> fnew=open('./12.txt','r+')  

>>> fnew.close()        

>>> fnew=open('./12.txt','r')

>>> fnew.read()

'abc\nthis is \nthis is add\nthisddd is add\nthisdddddddffggd is add\nharry is a boy'

文件里面有一个隐形的指针,当我们读取文件时隐形指针,从头移动到尾,指针正好在尾部,所以添加到末尾,当我们没有读取,指针在开头,我们写入时是从指针位置写入,所以会替换前面的内容

看如下例子:

>>> fenw=open('./12.txt','r+')

>>> fenw.write('DOT')

>>> fenw.close()

>>> fenw=open('./12.txt')    

>>> fenw.read()

'DOT\nthisddd is add\nharry is a boy\n'

由此可以发现我们写入的数据并没有追加到后面,而是替换了前面的数据

用迭代方法读取文件

>>> f1=open('./12.txt')

>>> s1=f1.read()

>>> s1

'DOT\nthisddd is add\nharry is a boy\n'

>>> f1.close()

>>> for i in open('./12.txt'):

...     print i

...

DOT


thisddd is add


harry is a boy

文件对象方法

readline:

string=FileObject.readline([size])

说明:每次读取文件的 一行

   size是指每行每次读取size个字节,直到行的末尾

例子:

>>> fnew=open('./12.txt','r')

>>> fnew.readline()

'DOT\n'

>>> fnew.readline()

'thisddd is add\n'

>>> fnew.readline()

'harry is a boy\n'

>>> fnew.readline()

''

>>> fnew.readline()

''


readlines 是把每一行当做列表的一个元素,所有的行返回一个列表,这样可以把列表迭代

例子

>>> fnew=open('./12.txt','r')

>>> fnew.readlines()        

['DOT\n', 'thisddd is add\n', 'harry is a boy\n']

next:

格式:FileObject.next()

说明:返回当前行,并将指针指导下一行

next类似,readline但是readline读取到文件末尾后,会无限读取空字符串,next独到结尾会提示迭代终止

>>> fnew=open('./12.txt','r')

>>> fnew.next()              

'DOT\n'

>>> fnew.next()

'thisddd is add\n'

>>> fnew.next()

'harry is a boy\n'

>>> fnew.next()

Traceback (most recent call last):

 File "<stdin>", line 1, in <module>

StopIteration


writelines

格式:

FileObject.writelines(List)

说明:

多行写

效率比write高,速度更快,少量写入可以使用write

例子:

>>> L=['one\n','two\n','three\n']

>>> f1=open('./12.txt','a')

>>> f1.writelines(L)

>>> f1.close()

>>> f1=open('./12.txt')

>>> f1.read()

'DOT\nthisddd is add\nharry is a boy\none\ntwo\nthree\n'

FileObject.seek(偏移量,选项)

选项=0是,表示将文件指针指向从文件头部到“偏移量”字节处。

选项=1时,表示将文件直指向从文件当前位置,向后移动“偏移量”字节处

选项=2时,表示将文件指针指向从文件的尾部,向前移动“偏移量”字节。

注:提交更新,平时我们写入数据后必须关闭文件才能看到写入的数据,我们用flush进行提交更新这样,不用关闭就可以看到数据。

>>> f1=open('./12.txt','a')

>>> l=['a','b']

>>> f1.writelines(l)

>>> f1.flush

<built-in method flush of file object at 0xb77a5c28>

>>> f1=open('./12.txt')    

>>> f1.read()

'DOT\nthisddd is add\nharry is a boy\none\ntwo\nthree\n'