文件操作

上一篇 / 下一篇  2017-08-28 22:10:18 / 个人分类:python

r”模式打开文件

1)不存在该文件会报错

>>> f=open("f:/8.txt")

Traceback (most recent call last):

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

IOError: [Errno 2] No such file or directory: 'f:/8.txt'

2)不能向该文件写入内容

>>> f=open("f:/3.txt")

>>> f.write("d")

Traceback (most recent call last):

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

IOError: File not open for writing

>>>

3)可以读取文件的内容

>>> for i in f:

...     print i,

...

zhang

cong

cong

zhagnjie

>>>

r+”模式打开文件:

1)不存在该文件会报错

>>> f=open("f:/8.txt","r+")

Traceback (most recent call last):

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

IOError: [Errno 2] No such file or directory: 'f:/8.txt'

>>>

2)可以往文件中写入内容,写入内容从第一个字符开始替换,可以读取文件内容

>>> f=open("f:/3.txt","r+")

>>> for i in f:

...     print i,

...

zhang

zhagnjie

koko

>>> f.write("kokok")    #打开后进行了遍历读取,已经把指针放在了文件的最后,所以变成了追加

>>> f.close()

>>> f=open("f:/3.txt","r+")

>>> for i in f:

...     print i,

...

zhang

zhagnjie

koko

kokok

>>>

#实际上以r+打开的文件,写入内容时是从一个字符开始替换的

>>> f=open("f:/3.txt")

>>> for i in f:

...     print i,

...

098456789987

>>> f=open("f:/3.txt",'r+')

>>> f.write("zhang")

>>> f.close()

>>> f=open("f:/3.txt")

>>> for i in f:

...     print i,

...

zhang6789987

>>>

w模式打开文件:

1)不能读取文件内容

>>> f=open("f:/3.txt","w")

>>> for i in f:

...     print i,

...

Traceback (most recent call last):

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

IOError: File not open for reading

>>>

2)打开不存在的文件时,会自动创建新文件

>>> f=open("f:/8.txt","w")

>>>

3)打开存在的文件时,会清空文件的内容,从新开始写入

>>> f=open("f:/3.txt","w")  #清空了之前的文件的内容,从头开始写入

>>> f.write("zccjason")

>>> f.close()

>>> f=open("f:/3.txt")

>>> for i in f:

...     print i,

...

zccjason

w+的模式打开文件,,可读可写

1)可以读取打开的文件,此时读取文件的内容为空,因为打开已经存在的文件时,之前的内容会被清空

>>> f=open("f:/3.txt","w+")

>>> for i in f:

...     print i,

...

>>>

2)打开不存在的文件时,会自动创建新文件

>>> f=open("f:/8.txt","w+")

>>> f.write("zhang")

>>> f.close()

>>> f=open("f:/8.txt")

>>> for i in f:

...     print i,

...

zhang

>>>

文件操作

r+模式,能读能写,从一个字符开始覆盖

>>> f=open("f:\\6.txt","r+")
>>> f.read()
'zhang\r\ncong\r\n'
>>> print f.read()

>>> f.seek(0)
>>> print f.read()
zhang
cong
>>> f.write("ZHAGN")    #“r+”模式打开文件,在文件末尾写入时会报错,好像是因为要覆盖,溢出了
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 0] Error

>>> f.seek(0)
>>> f.write("ZHAGN")
>>> f.seek(0)
>>> print f.read()
ZHAGN
cong

w+模式,能都能写,先清空再写入

f.seek()

>>> f=open("f:\\e.txt","w+")
>>> f.write("fsdafasdf")   #写入内容
>>> f.tell()  #返回当前的文件位置索引
9L
>>> f.seek(4,2)  #再文件末尾向后移4
>>> f.write("jklkj")  #再文件末尾的第5位开始写入内容
>>> f.seek(0)  #把文件的位置索引指向文件的开头
>>> print f.read()    #读取文件
fsdafasdf    jklkj
>>> f.tell()
18L
>>> f.seek(2,1)  #在当前的文件索引值往后移2
>>> f.tell()
20L
>>> f.seek(-3,1)  #在当前的位置索引值往前移三位
>>> f.tell()
17L
>>>

文件存储的编码格式:

存的是utf-8

>>> f=open("f:\\a.txt","w")

>>> f.write(u"问哦们".encode("utf-8"))

>>> f.close()

存的是ansi

>>> f=open("f:\\b.txt","w")

>>> f.write(u"问哦们".encode("gbk"))

>>> f.close()

存的是ansi

>>> f=open("f:\\a.txt","w")

>>> f.write("问哦们")

>>> f.close()


TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-18  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 14440
  • 日志数: 20
  • 建立时间: 2016-10-19
  • 更新时间: 2018-01-27

RSS订阅

Open Toolbar