third day- 01--文件操作

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'\xd6\xd0\xb9\xfa'
3 # s2 = s1.decode('gbk')
4 # s3 = s2.encode('utf-8')
5 # print(s3)  # b'\xe4\xb8\xad\xe5\x9b\xbd'
6 # s1 = b'\xd6\xd0\xb9\xfa'.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.文件操作

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')
文件的改

猜你喜欢

转载自www.cnblogs.com/lijie123/p/8856840.html
今日推荐