• 文件内指针移动


    # _*_ coding: utf-8 _*_

    # 大前提:
    # 文件内指针的移动是按Bytes为单位的,
    # 唯读t模式下的read读取内容个数是以字符为单位

    # with open('a.txt',mode='rt',encoding='utf-8') as f:
    # data = f.read(3) #三个字符
    # print(data)
    #
    # with open('a.txt',mode='rb') as f:
    # data = f.read(3)
    # print(data)
    # print(data.decode('utf-8'))

    # f.seek(指针移动的Bytes数,模式控制):控制文件指针的移动
    # 模式控制:
    # 0(t): 默认的模式,该模式代表指针移动的字节数是以文件开头为参照的
    # 1(b): 该模式代表指针移动的字节数是以当前所在的位置为参照的
    # 2(b): 该模式代表指针移动的字节数是以文件末尾的位置为参照的
    # 强调:其中0模式可以在t或者b模式使用,而1跟2模式只能在b模式下用

    # f.tell()查看文件指针当前距离文件开头的位置

    # 0模式详解
    # with open('a.txt', mode='rt', encoding='utf-8') as f:
    # f.seek(7, 0) # t模式下的0模式 就是按字符是以文件开头为参照的
    # print(f.tell())
    # print(f.read())

    # with open('a.txt', mode='rb') as f:
    # f.seek(3,0)
    # print(f.tell())
    # print(f.read().decode('utf-8'))

    # with open('a.txt',mode='rt',encoding='utf-8') as f:
    # f.seek(7,0)
    # print(f.read())

    # 1模式详解
    # with open('a.txt',mode='rb') as f:
    # f.seek(3,1)
    # print(f.tell()) #查看文件指针当前距离文件开头的位置
    # f.seek(4,1)
    # print(f.tell())
    # print(f.read().decode('utf-8'))

    # 2模式详解
    # with open('a.txt', mode='rb') as f:
    # f.seek(-10,2) #有换行符 1个Bytes
    # data = f.read()
    # print(data.decode('utf-8'))

    with open('access.log',mode='rb') as f:
    f.seek(0,2)
    while True:
    line = f.readline()
    if len(line) == 0:
    continue
    else:
    print(line.decode('utf-8'),end='')
  • 相关阅读:
    Elasticsearch 索引文档如何使用自动生成 Id?
    Spring Boot 缓存 知识点
    table的各种用法
    Spring Boot 整合 Elasticsearch
    Spring Boot 集成 Kafka
    Spring Boot 2实现分布式锁——这才是实现分布式锁的正确姿势!
    Spring Cloud 与 Spring Boot 版本兼容关系
    Spring Boot 之:Spring Boot Admin
    JVM 性能调优工具
    Spring Boot 之:Actuator 监控
  • 原文地址:https://www.cnblogs.com/OutOfControl/p/9688631.html
Copyright © 2020-2023  润新知