Skip to content
Think & Reflect · Q3

Q.Does the seek() function work in the same manner for text and binary files?

Yanam CbseNCERTSubjective· 2mImportance★★★★★
77% · 17/22 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

No, the seek() function does not work in the same manner for text and binary files due to differences in how file positions are managed and the impact of character encoding in text files.

The seek() function is used to change the current position of the file pointer within a file. This pointer determines where the next read or write operation will occur. While the fundamental purpose is the same, its behavior and permissible arguments differ significantly between text and binary files in Python.

seek() for Binary Files

When working with binary files (opened in modes like 'rb', 'wb', 'ab', 'rb+', etc.), seek() operates on byte offsets. This means you can move the file pointer to any specific byte position within the file.

The syntax for seek() in binary mode is typically file_object.seek(offset, whence).

  • offset: An integer representing the number of bytes to move.
  • whence: An optional integer argument that specifies the reference point for the offset.
    • 0 (or os.SEEK_SET): The offset is relative to the beginning of the file. This is the default value.
    • 1 (or os.SEEK_CUR): The offset is relative to the current file pointer position.
    • 2 (or os.SEEK_END): The offset is relative to the end of the file. In this case, the offset should typically be negative to move backward from the end.

For example, f.seek(100, 0) moves the pointer 100 bytes from the beginning. f.seek(-50, 2) moves the pointer 50 bytes backward from the end of the file.

seek() for Text Files

When working with text files (opened in modes like 'r', 'w', 'a', 'r+', etc.), seek() behaves differently and has significant restrictions. This is because text files involve character encoding (e.g., UTF-8, UTF-16). A single character might be represented by one, two, or more bytes, and this can vary. If you were to seek() to an arbitrary byte offset, you might land in the middle of a multi-byte character, leading to decoding errors or corrupted data.

To avoid these issues, seek() in text mode is restricted:

  • The whence argument must be 0 (relative to the beginning of the file). You cannot use 1 (current position) or 2 (end of file).
  • The offset argument must be either 0 (to go to the very beginning of the file) or a value previously returned by the tell() function. The tell() function in text mode returns an opaque "cookie" that represents a valid position within the file, which seek() can then safely use. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.