LoginSignup
0
2

More than 3 years have passed since last update.

seek

Posted at
s = """\
AAA
BBB
CCC
DDD
"""

with open('test.txt', 'w') as f:
    f.write(s)

with open ('test.txt', 'r') as f:
    print(f.tell())
    print(f.read(1))
    f.seek(5)
    print(f.read(1))
    f.seek(14)
    print(f.read(1))
    f.seek(15)
    print(f.read(1))
    f.seek(5)
    print(f.read(1))
実行結果
0
A
B
D


B
print(f.tell())

で現在地を出力

print(f.read(1))

で現在地0から1文字を出力
これはAにあたる

f.seek(5)

で現在地を5に移動させ、

print(f.read(1))

で現在地5から1文字を出力
これはBにあたる

位置
0, 1, 2, 3
4, 5, 6, 7
8, 9, 10, 11
12, 13, 14, 15

文字
A, A, A, 改行
B, B, B, 改行
C, C, C, 改行
D, D, D, 改行

と配置されている。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2