zoukankan      html  css  js  c++  java
  • 🍖文件指针移动

    一.指针移动的单位

    • 除了 t 模式下的 read(n) 中的 n 代表的是字符个数

    • 其余的移动单位都是以字节为单位

    先向文件里写入一串字符
    with open(r"test.txt", "w+b")as f:
         f.write("abc你好".encode("utf-8"))
    
     "t"模式下的"read(n)"读得是字符个数
    with open('test.txt',mode='rt',encoding='utf-8') as f:
        res=f.read(5)   # 读5个字符
        print(res)      # abc你
        
     非"t"模式下的"read(n)"读得是 Bytes 个数   
    with open('test.txt', mode='rb') as f:
        res = f.read(6)                     # 读6个 Bytes
        print(res.decode("utf-8"))          # abc你
    

    二. f.seek( ) : 偏移量

    1.格式说明

    • f.seek([移动的字节格式],[模式])

    2.三种模式

    • 0 : 参照文件开头移动指针
    • 1 : 参照当前所在的位置移动指针
    • 2: 参照文件末尾位置移动指针

    ps : 只有在 0 模式可以在 t 下使用, 12 只能在 b 下使用

    with open('test.txt', mode='rt', encoding='utf-8') as f:
        f.seek(3, 0)   # 指针跳到文件开头,然后向右移动三个字节
        f.seek(3, 1)   # "t" 模式下不支持 "1",报错
        f.seek(-3, 2)  # "t" 模式下不支持 "2",报错
    
    1. f.tell( ): 获取当前指针位置

    with open('test.txt', mode='rb') as f:
        f.seek(3, 0)      # 指针跳到文件开头,然后向右移动三个字节
        print(f.tell())   # 3  (光标处于"abc"后面)
        f.seek(3, 1)      # 指针在当前停留的位置向右移动三个字节
        print(f.tell())   # 6  (光标处于"abc你"后面)
        f.seek(-7, 2)     # 指针跳到末尾向左移动七个字节
        print(f.tell())   # 2  (光标处于"ab"后面)
    

    三.模拟 Linux 的 tail -f命令效果

    1.tail -f [文件名]

    • 格式 : tail -f [文件名]

    • 功能 : 从末尾实时查看一个文件的新增内容

    2.开始实现

    • 模拟一个日志文件,里面不断的在写入日志
    import time
    
    count = 1
    while True:
        with open(r"log.txt","at",encoding="utf-8")as f:
            f.write(f"这是第{count}条日志\n")
            count +=1
            time.sleep(2)
    
    • 模拟 tail -f 功能实时监控
    import time
    
    with open(r"log.txt","rb")as f:
        f.seek(0,2)
        while 1:
            line = f.read()
            if len(line) == 0:
                time.sleep(0.2)
                continue
    
            print(line.decode("utf-8"),end="")
            time.sleep(0.3)
    

    3.使用函数封装 (后一章知识点)

    • 监听任一文件的末尾是否增加数据
    import os,time
    
    def tail(file_path):
        if not os.path.isfile(file_path):           #判断文件是否存在
            return "文件不存在"
        with open(rf'{file_path}',"rb")as read_f:
            read_f.seek(0,2)
            while True:
                res = read_f.readline()            #判断取出的是否为空
                if len(res) == 0:
                    time.sleep(0.2)
                    continue
                print(res.decode("utf-8"),end="")
                time.sleep(0.2)
    
    res = tail("H:\Pycharm File\PycharmProjects\python正课\day07\log.txt")
    print(res)
    

    四.指针移动小练习

    1.使用 w 模式创建文件,内容"abc你好"

    with open(r"testtt.txt", "wt", encoding="utf-8")as f:
        f.write("abc你好\n")
    

    2."r + " 模式练习

    • 读全部内容
    • 读出‘你好’
    • 读出‘abc’
    • 把‘abc’覆盖成‘ABC’
    • 把‘你好’覆盖成‘你坏’
    with open(r"testtt.txt", "r+", encoding="utf-8")as f:
        print(f.read())   # abc你好
        
        f.seek(3, 0)
        print(f.tell())   # 3
        print(f.read())  # 你好
        
        f.seek(0, 0)
        print(f.tell())   # 0
        print(f.read(3))  # abc
        
        f.seek(0, 0)
        print(f.tell())   # 0
        f.write("ABC")  
        f.seek(0, 0)
        print(f.read())   # ABC你好
    
        f.seek(6, 0)
        print(f.tell())  # 6
        f.write("坏")
        f.seek(0, 0)
        print(f.read(5)) # abc你坏
    

    3."w+b" 模式练习

    • 读全部内容
    • 读出‘你好’
    • 读出‘abc’
    • 读出'c',再读出’好‘
    • 把‘c’覆盖成’C‘
    • 把‘好’覆盖层’坏‘
    先写一串字符进文件
    with open(r"testtt.txt", "w+b")as f:
        f.write("abc你好".encode("utf-8"))
        
        print(f.read().decode("utf-8"))  # abc你好
        
        f.seek(-6, 2)
        print(f.tell())                  # 3
        print(f.read().decode("utf-8"))  # 你好
        
        f.seek(0, 0)
        print(f.tell())                  # 0
        print(f.read(3).decode("utf-8")) # abc
        
        f.seek(2, 0)                     # 2
        print(f.tell())
        print(f.read(1).decode("utf-8")) # c
        f.seek(-3, 2)
        print(f.tell())                  # 6
        print(f.read().decode("utf-8"))  # 好
        
        f.seek(2, 0)
        f.write(b'C')           
        f.seek(0, 0)  
        print(f.read().decode("utf-8"))  # abC你好
        
        f.seek(6, 0)
        f.write('坏'.encode("utf-8"))
        f.seek(0, 0)
        print(f.read().decode("utf-8"))  # abC你坏
    

    4."a + b" 模式练习

    "a" 模式下,不管你的指针跳到了那里,写入的内容都是在文件末尾
    with open(r"testtt.txt", "a+b")as f:
        f.seek(0, 0)
        f.write(b'DDD')
        f.seek(0, 0)
        print(f.read().decode("utf-8"))  #abC你坏DDD
    
  • 相关阅读:
    #Eclipse web工程 部署 三种方式 3
    Eclipse web工程 部署 三种方式 2
    浅谈Eclipse调用Tomcat服务的原理
    Eclipse web工程 部署 三种方式 1
    java区分绝对路径和相对路径
    设置checkbox不能选中,复选框不能选中
    Java IO,io,文件操作,删除文件,删除文件夹,获取文件父级目录
    NFS,两台linux设置网络共享文件系统
    dom4j 改变XML声明和编码格式
    笔记本自带wifi
  • 原文地址:https://www.cnblogs.com/songhaixing/p/14052837.html
Copyright © 2011-2022 走看看