The seek() method can be used to position the file object at a particular position in a file depending upon the offset. What will be the offset position, if someone wants to write at the current position of the file?
The seek() method can be used to position the file object at a particular position in a file depending upon the offset. What will be the offset position, if someone wants to write at the current position of the file?
Solution
In Python, seek(offset, whence) takes a second argument that sets the reference point for the offset. The value means the current file position, so f.seek(0, 1) keeps the file object where it is and writing continues from there.
Option 1 (): the beginning of the file, so writing would start from the start.
Option 3 (): the end of the file, which is used to append.
The values for whence are (start), (current) and (end). Remember them as 0-1-2 for start-current-end.
Related questions:
2025: 27 May Shift 2
2026: 11 May Shift 2
2025: 14 May Shift 1
2026: 24 May Shift 1
2026: 21 May Shift 1