30/09/2018, 23:50

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

file.seek(offset[, whence]) hoạt động ntn?

Lương Quang Mạnh viết 01:56 ngày 01/10/2018

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.
htl@PyMI.vn viết 01:55 ngày 01/10/2018

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

Lương Quang Mạnh viết 01:55 ngày 01/10/2018

Đâ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í.

htl@PyMI.vn viết 01:58 ngày 01/10/2018

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

Lương Quang Mạnh viết 02:01 ngày 01/10/2018

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

  • 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()
htl@PyMI.vn viết 02:04 ngày 01/10/2018

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

Lương Quang Mạnh viết 01:57 ngày 01/10/2018

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

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()

htl@PyMI.vn viết 02:00 ngày 01/10/2018

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]: 
Bài liên quan
0