08文件其他模式及光标移动

一、其他内容补充

“+”表示可以同时读写某个文件

r+,可读可写

w+,可读可写

a+,可读可写

1.“r+”(r+t)

 with open(r'test',mode='r+',encoding='utf-8') as f:
     print(f.readable())
     print(f.writable())
     print(f.readline())
     f.write('嘿嘿嘿')

r+模式里,write()写的内容永远在最后末尾(因为机械硬盘的圆盘结构)!

2.“w+”(r+t)

with open(r'test',mode='w+',encoding='utf-8') as f:
    # print(f.readable())
    # print(f.writable())
    f.write('123')
    print(f.readline())

w+模式中,程序一运行会清空文档内容;光标在最后;因此W+模式无法读到任何内容

3.“a+”(a+t)

with open(r'test',mode='w+',encoding='utf-8') as f:
    # print(f.readable())
    # print(f.writable())
    f.write('123')
    print(f.readline())

a+模式,在末尾write内容;光标在最后,因此无法读到任何内容。

4.“r+b”可读二进制

with open(r'test',mode='r+b') as f:
    print(f.readable())
    print(f.writable())
    res = f.read()
    # print(res.decode('utf-8'))
    res1 = str(res,encoding='utf-8')
    print(res1)

注意,r+b模式里,读出来的是二进制,加入解码(decode(‘utf-8’))即可看到中文。

转成中文的两种方式:

1.print(res.decode('utf-8'))

2.print(str(res,encoding=‘utf-8’))

print(res.decode('utf-8'))——print(type(res.decode('utf-8')))可以发现这是str类型,由二进制转为正常可看的str文本——所以可以写成第二种方式:str(res,encoding=‘utf-8’),这个格式由pycharm决定

 二、文件内光标的位置

1:知识补充

一个中文三个字节;一个英文或数字是一个字节

read括号内填写的数字:二进制字节数或字符串的个数。只在rt可读文本(rt)模式下为字符的个数,其他都是二进制的字节数

with open(r'test','rb') as f:
    res = f.read(6)  # 读的是三个字节bytes
    print(res)
    print(res.decode('utf-8'))

read()括号内的数字,只能大于或等于实际文本的字节。因为read是一次读完

2.文件内光标的移动。

涉及到遥控器的内置方法:seek()

f.seek(offset,whence)
seek移动都是字节。因此注意填写的数字的问题
offset:相对偏移量 光标移动的位数 whence: 0:参照文件的开头 t和b都可以使用 1:参照光标所在的当前位置 只能在b模式下用 2:参照文件的末尾 只能在b模式下使用

案例如下:
2.1rt模式:
with open(r'test','rt',encoding='utf-8') as f:
    print(f.read(1))
    # f.seek(6,0)  # seek移动都是字节数
    # f.seek(4,0)  # seek移动都是字节数
    # print(f.read(1))
    f.seek(0,0)
    print(f.read(1))

read()括号内输入的是读的“字符”个数

    2.2rb模式:

以鼠标当前位置和末尾位置

with open(r'test','rb') as f:
    print(f.read(3).decode('utf-8'))
    f.seek(3,1)
    print(f.read(1))
    f.seek(6,0)  # seek移动都是字节数
    f.seek(4,0)  # seek移动都是字节数
with open(r'test','rb') as f:
    print(f.read())
    f.seek(-4,2)
   #注意seek填的第一个数字
print(f.read().decode('utf-8'))

   2.3r+模式:

with open(r'test','r+',encoding='utf-8') as f:
    f.seek(3,0)
    f.write('w')

 注意r+模式中的write实际上是替换,因此必须注意字符个数的相同

三、写日志

import time
res = time.strftime('%Y-%m-%d %X')
# print(res,type(res))
#
with open(r'test01.txt','a',encoding='utf-8') as f:
    f.write('%s egon给jason发了1个亿的工资\n'%res)

四、监听文档输入

with open(r'test01.txt','rb') as f:
    # 先将光标移动到文件末尾
    f.seek(0,2)
    while True:
        res = f.readline()
        # 查看光标移动了多少位 bytes
        # print(f.tell())
        if res:
            print("新增的文件内容:%s"%res.decode('utf-8'))
            # 说明有人操作当前文件
        # else:
        #     # 说明文件没有被任何人操作
        #     print('暂无其他人操作该文件')

 五、截断文件

with open(r'test','a',encoding='utf-8') as f:
    f.truncate(6)  # 接收的字节的长度 整型
    # 保留0~6字节数 后面的全部删除(截断)

 六、修改数据

方式一:
1.修改文件
2.先将数据由硬盘读到内存(读文件)
3.在内存中完成修改(字符串的替换)
4.再覆盖原来的内容(写文件)
with open(r'C:\Program Files\feiq\Recv Files\day07\1\test01.txt','r',encoding='utf-8')as f:
    res=f.read()
    print(res,type(res))
with open(r'C:\Program Files\feiq\Recv Files\day07\1\test01.txt','w',encoding='utf-8')as f:
    res2=res.replace('非洲','中国')
    print(res2)
    f.write(res2)
优点:任意时间硬盘上只有一个文件 不会占用过多硬盘空间
缺点:当文件过大的情况下,可能会造成内存溢出
文件修改方式2
创建一个新文件
循环读取老文件内容到内存进行修改 将修改好的内容写到新文件中
将老文件删除 将新文件的名字改成老文件名
import os

with open(r'test02.txt','r',encoding='utf-8') as read_f,\
        open(r'test02.swap','a',encoding='utf-8') as write_f:
    for line in read_f:
        new_line = line.replace('jason','egon')
        write_f.write(new_line)
os.remove('test02.txt')
os.rename('test02.swap','test02.txt')

 

posted @ 2019-07-08 19:18  xg1321  阅读(231)  评论(0编辑  收藏  举报