zoukankan      html  css  js  c++  java
  • 文件操作-seek read tell

    f.seek(offset,whence)
    offset: 相对偏移度 (光标移动的位数)针对的是字节
    whence:指定光标位置从何开始
        0:从文件开头
        1:从当前位置
        2:从文件末尾
    f.seek(
    6, 0) f.seek(-3, 2)
    简单的例子理解 seek read tell
    with open('e','r+',encoding='utf-8') as f2:
    f2.seek(3,0) #移动光标
    print(f2.read(2)) #读几个 光标也会向后移动几个
    print(f2.tell()) # 告诉位置

    # with open(r"a.txt",'r')as f: # 打开文件的编码:gbk # # print(f.read(2)) # print(f.tell()) # f.seek(5,0) # print(f.read(2)) # print(f.read(5)) with open(r"a.txt", 'rt',encoding='utf-8')as f: # 打开文件的编码:gbk print(f.read()) f.seek(6, 0) print(f.tell()) print(f.read(2)) # with open(r"a.txt",'r')as f: # 打开文件的编码:gbk # # print(f.read(2)) # print(f.tell()) # f.seek(4,0) # fuck的卡仕达看但扩大 # print(f.read(2).encode('utf-8')) # with open(r'a.txt', 'rb')as f: # print(f.read(10).decode('gbk')) # f.seek(10, 1) # print(f.tell()) # with open(r'a.txt', 'rb')as f: # f.seek(10, 1) # print(f.tell()) # with open(r'a.txt','rb')as f: # f.seek(-3,2) # print(f.tell())
  • 相关阅读:
    C语言I博客作业02
    第一次C语言作业
    C语言I博客作业02
    网页常用分享代码
    js生成验证码并验证
    js时间格式的转换
    Git 常用命令
    ASP.NET MVC中使用事务写法
    数据库游标导入数据
    js截取所需字符串长度
  • 原文地址:https://www.cnblogs.com/bigbox/p/11830410.html
Copyright © 2011-2022 走看看