python 基础---文件

读 r

 1 #对于r:存在文件直接读取文件内容
 2 f = open('世界杯.txt',mode='r',encoding='UTF-8')
 3 content = f.read()
 4 print(content)
 5 f.close()
 6 #对于rb:bytes类型,非文字类的文件需要读取
 7 f = open('世界杯',mode='rb',)
 8 content = f.read()
 9 print(content)
10 f.close()

写 w

#对于w:没有此文件就会创建文件
f = open('克罗地亚.txt',mode='w',encoding='UTF-8')
f.write('亚军')
f.close()

#对于w:有文件先将源文件的内容全部清除,在写。
f = open('克罗地亚.txt',mode='w',encoding='UTF-8')
f.write('测试')
f.close()

#wb:写进bytes类型
f = open('世界杯',mode='wb')
f.write('加文字'.encode('utf-8'))#这里必须写encode,前面加文字是str,使用encode转换成bytes
f.close()

追加 a

1 #追加 a
2 f = open('世界杯.txt',mode='a',encoding='UTF-8')
3 f.write('法国:克罗地亚')
4 f.close()
5 
6 #追加 ab,以bytes类型
7 f = open('世界杯',mode='ab')
8 f.write('法国:克罗地亚'.encode('utf-8'))
9 f.close()

 读写 r+

 1 # r+
 2 #读写
 3 #先读后写
 4 f = open('哦也',mode='r+',encoding= 'utf-8')
 5 print(f.read())
 6 f.write('法国:克罗地亚')
 7 f.close()
 8 #输出第一次信息
 9 #文件中:第一次信息法国:克罗地亚
10 
11 #先写后读
12 f = open('哦也',mode='r+',encoding= 'utf-8')
13 f.write('我要覆盖覆盖')
14 print(f.read())
15 f.close()
16 #输出国:克罗地亚
17 #原文件信息:我要覆盖覆盖国:克罗地亚
18 #光标在我要覆盖覆盖后面,即从国开始读取
19 
20 #r+b  (以bytes类型读写)
21 f = open('哦也',mode='r+b')
22 f.write('r+b的信息'.encode())
23 print(f.read())
24 f.close()
25 # 输出:b'\xe6\x81\xaf',open中有b,在open中就不需要encoding'utf-8'
26 #                        但是对文件进行写操作,就得用方法   字符.encode()

写读 w+

1 # w+
2 #写读
3 f = open('哦也',mode='w+',encoding='utf-8')
4 f.write('w+的信息')   #会清空源文件信息,再写
5 f.seek(0)              #调光标
6 print(f.read())
7 f.close()

a+

1 # a+,因为a操作里面只能追加,不能读
2 f = open('哦也',mode='a+',encoding='utf-8')
3 f.write('a+的信息')   
4 print(f.read())
5 f.close()

功能详解

read 按字符 读取

1  #read   按字符  读取
2 f= open('功能详解',mode='w',encoding='utf-8')
3 f.write('我是一只小毛驴')
4 f.close()
5 f = open('功能详解',mode='r+',encoding='utf-8')
6 content = f.read(3)  # 读出来的都是字符,最小单位
7 print(content)   #输出:  我是一
8 f.close()

seek 按字节 自己定光标

1 #seek    按字节   自己定光标
2 f = open('功能详解',mode='r+',encoding='utf-8')
3 f.seek(3)   #是按照字节定光标的位置,3个字节一个中文
4 content = f.read()
5 print(content)   #输出:  是一只小毛驴
6 f.close()

tell 告诉你光标位置

 1 #tell   告诉你光标位置
 2 f = open('功能详解',mode='r+',encoding='utf-8')
 3 f.seek(4)
 4 print(f.tell())  #输出: 4
 5 f.seek(5)
 6 print(f.tell())  #输出: 5
 7 f.close()
 8 
 9 #想读哪就读哪
10 #我是一只小毛驴
11 f = open('功能详解',mode='r+',encoding='utf-8')
12 f.seek(f.tell()+9)  #定位到第三个光标,从第四个位置开始读取,记住seek是以字节为单位,3字节1汉字
13 content = f.read()
14 print(content)   #输出: 只小毛驴
15 f.close()

其他

 1 f.readable()  #是否可读
 2 f.readline()  #一行一行读
 3 a = f.readline()  #一行一行读
 4 print(a)  #我是一只小毛驴
 5 
 6 f.readlines()  #一行一行读
 7 b = f.readlines()  #每一行当成列表中的一个元素
 8 print(b)   #输出:  ['我是一只小毛驴\n', '说了我也是']
 9 
10 f.truncate()  #对文件进行截取
11 c = f.truncate(0)  #()是位置
12 
13 #循环打印
14 for line in f:
15     print(line)
16 f.close()

with...open

1 #with open  可同时打开多个文件,不需要close()
2 with open('功能详解',mode='r+',encoding='utf-8') as f1:
3     print(f1.read())  #自动关闭文件
4 with open('功能详解',mode='r+',encoding='utf-8') as f1,open('功能详解',mode='w+',encoding='utf-8') as f2:
5    # print(f1.read())  # 自动关闭文件
6     print(f2.read())  # 自动关闭文件

 修改文件

 1 # 修改文件(读,写,删,重命名)
 2 with open('小护士班主任',encoding='utf-8') as f,open('小护士班主任.bak','w',encoding='utf-8') as f2:
 3     for line in f:
 4         if '星儿' in line:  #班主任:星儿
 5             line = line.replace('星儿','啊娇')
 6         #写文件
 7         f2.write(line) #小护士:金老板
 8 
 9 #删除文件,重命名文件
10 import os
11 os.remove('小护士班主任')                      #删除文件
12 os.rename('小护士班主任.bak','小护士班主任')  #重命名文件

 

 

 

 




posted @ 2018-07-16 13:26  前往Python取经之路  阅读(154)  评论(0编辑  收藏  举报