How do you move the file pointer to the byte from the beginning of a file in Python?
How do you move the file pointer to the byte from the beginning of a file in Python?
Solution
The syntax is file.seek(offset, whence). Here offset = 10 is the number of bytes to move, and whence = 0 means counting from the beginning of the file, so the pointer lands on byte 10 from the start.
Other values of whence: 1 is the current position, and 2 is the end of the file.
Option 2: the arguments are swapped, so it gives offset 0 and whence 10, which is invalid.
Option 3: file objects have no move() method.
We believe the answer could be Option 4 as well, since whence defaults to 0 in Python, so file.seek(10) behaves exactly like file.seek(10, 0). However, NTA's final answer key (after all challenges) says Option 1.
Related questions:
2025: 20 May Shift 1
2025: 22 May Shift 1
2025: 14 May Shift 1