Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bc47d7b

Browse files
committed
Merge: #16273: Fix tutorial discussion of seek/tell (opaque text-mode values).
2 parents f392c60 + 1c4e443 commit bc47d7b

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

Doc/tutorial/inputoutput.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,11 @@ first::
322322
>>> f.write(s)
323323
18
324324

325-
``f.tell()`` returns an integer giving the file object's current position in the
326-
file, measured in bytes from the beginning of the file. To change the file
327-
object's position, use ``f.seek(offset, from_what)``. The position is computed
325+
``f.tell()`` returns an integer giving the file object's current position in the file
326+
represented as number of bytes from the beginning of the file when in `binary mode` and
327+
an opaque number when in `text mode`.
328+
329+
To change the file object's position, use ``f.seek(offset, from_what)``. The position is computed
328330
from adding *offset* to a reference point; the reference point is selected by
329331
the *from_what* argument. A *from_what* value of 0 measures from the beginning
330332
of the file, 1 uses the current file position, and 2 uses the end of the file as
@@ -345,7 +347,10 @@ beginning of the file as the reference point. ::
345347

346348
In text files (those opened without a ``b`` in the mode string), only seeks
347349
relative to the beginning of the file are allowed (the exception being seeking
348-
to the very file end with ``seek(0, 2)``).
350+
to the very file end with ``seek(0, 2)``) and the only valid *offset* values are
351+
those returned from the ``f.tell()``, or zero. Any other *offset* value produces
352+
undefined behaviour.
353+
349354

350355
When you're done with a file, call ``f.close()`` to close it and free up any
351356
system resources taken up by the open file. After calling ``f.close()``,

0 commit comments

Comments
 (0)