Hỏi về cách hoạt động của hàm file.seek(offset[, whence])

Mọi người giải thích cho em hiểu cái: file.seek(offset[, whence]) hoạt động ntn?

Method này dùng để di chuyển vị trí con trỏ trong file đến offset.
Một ứng dụng rất điển hình là khi bạn muốn đọc lại một file object mà không muốn phải đóng rồi mở lại (file object là iterator, chỉ đọc được một lần, hết file là exhausted luôn), chỉ cần file.seek(0). Tức là di chuyển lên đầu file.
whence có thể là 0, 1, 2:

  • 0 (default): offset so với đầu file.
  • 1: offset so với vị trí hiện tại.
  • 2: offset so với cuối file.
3 Likes

Lưu ý là cần mở file ở chế độ 'b' nhé

1 Like

Đâu nhất thiết phải ở chế độ binary, text cũng dùng được mà. Dù cơ chế hoạt động hơi khác nhau một tí.

1 Like

không mở 'b' thì offset phải = 0

Đâu, mình thử với Python 3.5, dùng phà phà :unamused:

  • Offset luôn tính theo bytes nhưng có thể dùng với file object ở cả text mode lẫn binary mode.
  • Tuy nhiên, mở theo text mode thì whence bắt buộc phải bằng 0 (tính từ đầu file), tức là chỉ có thể sử dụng offset trả về bởi fileobj.tell()
1 Like

Vậy mở với textmode mà muốn offset 2 bytes từ cuối file thì làm ntn bạn?

Bên trên mình nói rồi còn gì.

1 Like

python 3.5 trên máy mình nó thế này:

Python 3.5.2 (default, Jul 17 2016, 00:00:00) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: with open('/etc/passwd') as f:
   ...:     f.seek(1, 0)
   ...:     print(f.readline())
   ...:     
oot:x:0:0:root:/root:/bin/bash


In [2]: with open('/etc/passwd') as f:
   ...:     f.seek(1, 1)
   ...:     print(f.readline())
   ...:     
---------------------------------------------------------------------------
UnsupportedOperation                      Traceback (most recent call last)
<ipython-input-2-97b74952eee8> in <module>()
      1 with open('/etc/passwd') as f:
----> 2     f.seek(1, 1)
      3     print(f.readline())
      4 

UnsupportedOperation: can't do nonzero cur-relative seeks

In [3]: with open('/etc/passwd') as f:
   ...:     f.seek(0, 1)
   ...:     print(f.readline())
   ...:     
root:x:0:0:root:/root:/bin/bash


In [4]: with open('/etc/passwd') as f:
   ...:     f.seek(1, 2)
   ...:     print(f.readline())
   ...:     
---------------------------------------------------------------------------
UnsupportedOperation                      Traceback (most recent call last)
<ipython-input-4-036a9cd2ad6a> in <module>()
      1 with open('/etc/passwd') as f:
----> 2     f.seek(1, 2)
      3     print(f.readline())
      4 

UnsupportedOperation: can't do nonzero end-relative seeks

In [5]: with open('/etc/passwd') as f:
   ...:     f.seek(0, 2)
   ...:     print(f.readline())
   ...:     


In [6]: 
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?