third day- 01--文件操作

 1 1、请分别介绍文件操作中不同的打开方式之间的区别:
 2 
 3 模式 含义
 4 r       文本只读模式
 5 rb       二进制模式  #这种方法,是用来传输或存储,不给人看的。
 6 r+      r+ 是读写模式,只要沾上r,文件必须存在
 7 rb+      二进制读写模式
 8 w       w 写模式,它是不能读的,如果用w模式打开一个已经存在的文件,会清空以前的文件内容,重新写
 9 wb      和wb 两种模式
10 w+      w+ 是读写内容,只要沾上w,肯定会清空原来的文件
11 wb+     二进制写读模式
12 a       a 追加模式,也能写,在文件的末尾添加内容
13 ab      二进制追 加模式
14 a+
15 ab+
16 '''
从别处拷贝

1.文件操作基本流程

#1. 打开文件,得到文件句柄并赋值给一个变量
f=open('a.txt','r',encoding='utf-8') #默认打开模式就为r

#2. 通过句柄对文件进行操作
data=f.read()

#3. 关闭文件
f.close()
护士主妇空姐老师联系方式.txt
        1,文件路径
        2,编码方式:utf-83,动作mode,读,读写,写读.....

    f1 = open('D:空姐护士老师主妇.txt', encoding='utf-8', mode='r')
    content = f1.read()
    print(content)
    f1,文件句柄,文件对象,file,f_handle,file_handle,f_obj
    open打开的指令,windows的指令,
    windows 默认编码方式gbk,linux默认编码方式utf-8,mac utf-81,打开文件,产生文件句柄。
    2,操作文件句柄。
    3,关闭文件。
注意:
    SyntaxError: (unicode error) 'unicodeescape' codec
     can't decode bytes in position 2-3: truncated UXXXXXXXX escape
     f1 = open(r'D:空姐护士老师主妇.txt', encoding='utf-8', mode='r')
     # f1 = open('D:\空姐护士老师主妇.txt', encoding='utf-8', mode='r')
    EncodeDecodeErorr: 编码错误。
View Code

调用文件的两个小例子

#相对路径下,不用写路径,可以直接写文件名
# f1=open('123.py',encoding='utf-8',mode='r')  #mode可以不写,默认读
# content=f1.read()  #read全部读出
# print(content)
# f1.close()
#绝对路径下
# f1=open('F:\Python自动化21期\day03\1.txt',encoding='utf-8') #注意路径是\,否则会报错
# content=f1.read()
# print(content)
# f1.close()
View Code

2.文件打开模式

文件句柄 = open(‘文件路径’,‘模式’)

rb模式 非文字类的文件的操作。不写encoding

unicode ---> bytes encode()
bytes---> unicode decode()

1 #编码的补充:
2 # s1 = b'xd6xd0xb9xfa'
3 # s2 = s1.decode('gbk')
4 # s3 = s2.encode('utf-8')
5 # print(s3)  # b'xe4xb8xadxe5x9bxbd'
6 # s1 = b'xd6xd0xb9xfa'.decode('gbk').encode('utf-8')  简化写法
7 # print(s1)
编码的补充

#1. 打开文件的模式有(默认为文本模式):
r ,只读模式【默认模式,文件必须存在,不存在则抛出异常】
w,只写模式【不可读;不存在则创建;存在则清空内容】
a, 只追加写模式【不可读;不存在则创建;存在则只追加内容】

#2. 对于非文本文件,我们只能使用b模式,"b"表示以字节的方式操作(而所有文件也都是以字节的形式存储的,使用这种模式无需考虑文本文件的字符编码、图片文件的jgp格式、视频文件的avi格式)
rb 
wb
ab
注:以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型,不能指定编码

#3,‘+’模式(就是增加了一个功能)
r+, 读写【可读,可写】
w+,写读【可写,可读】
a+, 写读【可写,可读】

#4,以bytes类型操作的读写,写读,写读模式
r+b, 读写【可读,可写】
w+b,写读【可写,可读】
a+b, 写读【可写,可读】
View Code

3.文件操作

read(3):

  1. 文件打开方式为文本模式时,代表读取3个字符

  2. 文件打开方式为b模式时,代表读取3个字节

其余的文件内光标移动都是以字节为单位的如:seek,tell,truncate

注意:

  1. seek有三种移动方式0,12,其中1和2必须在b模式下进行,但无论哪种模式,都是以bytes为单位移动的

  2. truncate是截断文件,所以文件的打开方式必须可写,但是不能用w或w+等方式打开,因为那样直接清空文件了,所以truncate要在r+或a或a+等模式下测试效果。
View Code

1.read 全部读取

read(n) 按照字符读取

readline()按行读取

readlines() 将每一行作为列表的一个元素并返回这个列表

for循环

# read 全部读出
# f1 = open('log1', encoding='utf-8')
# content = f1.read()  #
# print(content)
# f1.close()

#read(n)
# f1 = open('log1', encoding='utf-8')
# content = f1.read(5)  # r 模式 按照字符读取。
# print(content)
# f1.close()

# f1 = open('log1', mode='rb')
# content = f1.read(3)  # rb模式 按照字节读取。utf-8编码,写4会报错
# print(content.decode('utf-8'))
# f1.close()

#readline()按行读取
# f1 = open('log1', encoding='utf-8')
# print(f1.readline())
# print(f1.readline())
# print(f1.readline())
# print(f1.readline())
# f1.close()

#readlines() 将每一行作为列表的一个元素并返回这个列表
# f1 = open('log1', encoding='utf-8')
# print(f1.readlines())
# f1.close()

#for循环  最好只占一行空间,直接read占用内存
# f1 = open('log1', encoding='utf-8')
# for i in f1:
#     print(i)
# f1.close()
View Code

#r+ 读写

#w模式  覆盖之前的内容重新写入输入的内容
 w+写读模式  清空之前内容写入
a 追加写
 1 # r+读写
 2 # f1=open('123.py',encoding='utf-8',mode='r+')
 3 # print(f1.read())
 4 # f1.write('666')  #在文件的最后一行后面写入了666
 5 # f1.close()
 6 
 7 # f1=open('123.py',encoding='utf-8',mode='r+')
 8 # f1.seek(0,2) #调至最后,按照字节去调整光标
 9 # f1.write('777')
10 # f1.seek(0)  #调整光标  不写打印出内容为空!
11 # print(f1.read())
12 # #光标 按照字节去运转 seek
13 # f1.close()
14 
15 #w模式  覆盖之前的内容重新写入输入的内容
16 #w和wb区别????于非文本文件,我们只能使用b模式
17 # f1=open('123.py',encoding='utf-8',mode='w')
18 # f1.write('你真美,,')
19 # f1.close()
20 # f1=open('123.py',mode='wb')
21 # f1.write('1234567q释放'.encode('utf-8'))
22 # f1.close()
23 
24 #  # w+写读模式  清空之前内容写入
25 # f1=open('123.py',encoding='utf-8',mode='w+')
26 # print(f1.read())  #清空之前内容
27 # f1.write('666')  #写入
28 # f1.seek(0)  #展示写入的内容,加这两行
29 # print(f1.read())  #展示写入的内容,加这两行
30 # f1.close()
31 
32 #a 追加写 ab
33 # f1=open('123.py',encoding='utf-8',mode='a')
34 # f1.write('1234')
35 # f1.close()
36 # ab????于非文本文件,我们只能使用b模式
37 #a+  可以读取
38 # f1=open('123.py',encoding='utf-8',mode='a')
39 # f1.write('lijie')
40 # f1.seek(0)
41 # print(f1.read())
42 # f1.close()
View Code

2.其它操作方法

 1 #其他操作方法:
 2 #read read(n) readline() readlines() write() close
 3 # readable??是否可读
 4 # writable??是否可写
 5 #tell 告诉指针的位置
 6 # f1=open('123.py',encoding='utf-8',mode='w')
 7 # f1.write('你真美1')
 8 # print(f1.tell())
 9 # f1.close()
10 #seek(参数),seek(0,2) 调至最后 按照字节去调整光标
11 
12 # 不手动关闭文件方法  with open() as:
13 # with open ('123.py',encoding='utf-8',mode='r') as f1,
14 #         open ('2.py',encoding='utf-8',mode='w') as f2: #文件不存在时自动创建
15 #     print(f1.read())
16 #     f2.write('234')
View Code

4.文件的改

 1 #文件的改
 2 #1,打开原文件,产生文件句柄。
 3 #2,创建新文件,产生文件句柄。
 4 #3,读取原文件,进行修改,写入新文件。
 5 #4,将原文件删除。
 6 #5,新文件重命名原文件。
 7 
 8 # import os
 9 # with open('file_test', encoding='utf-8') as f1,
10 #     open('file_test.bak', encoding='utf-8', mode='w') as f2:
11 #     old_content = f1.read()#此方法全部读取,不好
12 #     new_content = old_content.replace('alex','SB')
13 #     f2.write(new_content)
14 # os.remove('file_test')
15 # os.rename('file_test.bak','file_test')
16 
17 
18 import os
19 with open('file_test', encoding='utf-8') as f1,
20     open('file_test.bak', encoding='utf-8', mode='w') as f2:
21     for line in f1:  #for循环未结束一直在持续写,所以不覆盖
22         new_line = line.replace('SB','alex')
23         f2.write(new_line)
24 os.remove('file_test')
25 os.rename('file_test.bak','file_test')
文件的改

 5.复习

 1 # 文件操作
 2     # 打开文件
 3         # f = open('文件路径')  默认的打开方式r ,默认的打开编码是操作系统的默认编码
 4         # r w a (r+ w+ a+) 以上6种加b  ,如果打开模式+b,就不需要指定编码了
 5         # 编码 UTF-8 ,gbk
 6     # 操作文件
 7         # 读
 8             # read 不传参数 意味着读所有
 9                 # 传参,如果是r方式打开的,参数指的是读多少个字符
10                 # 传参,如果是rb方式打开的,参数指的是读多少个字节
11             # readline
12                 # 一行一行读  每次只读一行,不会自动停止
13             # for循环的方式
14                 # 一行一行读  从第一行开始 每次读一行 读到没有之后就停止
15         # 写
16             # write 写内容
17     # 关闭文件
18         # f.close()
19     # with open() as f:
20     # 修改文件 :
21         # import os
22         # os.remove
23         # os.rename
View Code
原文地址:https://www.cnblogs.com/lijie123/p/8856840.html