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())
  • 相关阅读:
    加一
    斐波那契数
    整数的各位积和之差
    移除元素
    删除排序数组中的重复项
    有效的括号
    爬楼梯
    最长公共前缀
    罗马数字转整数
    回文数
  • 原文地址:https://www.cnblogs.com/bigbox/p/11830410.html
Copyright © 2011-2022 走看看