From b61cc0ace318aa7e25a06e8931dc4875ed795b0c Mon Sep 17 00:00:00 2001 From: Bonu Krishna Chaitanya Date: Sat, 13 Sep 2025 11:53:28 +0530 Subject: [PATCH 1/2] add hyperlinks to methods of file objects --- Doc/tutorial/inputoutput.rst | 47 ++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst index a00f06cf46c41a..7d23293b0a28ef 100644 --- a/Doc/tutorial/inputoutput.rst +++ b/Doc/tutorial/inputoutput.rst @@ -395,26 +395,26 @@ Methods of File Objects The rest of the examples in this section will assume that a file object called ``f`` has already been created. -To read a file's contents, call ``f.read(size)``, which reads some quantity of -data and returns it as a string (in text mode) or bytes object (in binary mode). -*size* is an optional numeric argument. When *size* is omitted or negative, the -entire contents of the file will be read and returned; it's your problem if the -file is twice as large as your machine's memory. Otherwise, at most *size* -characters (in text mode) or *size* bytes (in binary mode) are read and returned. -If the end of the file has been reached, ``f.read()`` will return an empty -string (``''``). :: +To read a file's contents, call :meth:`f.read(size) `, which +reads some quantity of data and returns it as a string (in text mode) or bytes +object (in binary mode). *size* is an optional numeric argument. When *size* is +omitted or negative, the entire contents of the file will be read and returned; +it's your problem if the file is twice as large as your machine's memory. +Otherwise, at most *size* characters (in text mode) or *size* bytes +(in binary mode) are read and returned. If the end of the file has been reached, +``f.read()`` will return an empty string (``''``). :: >>> f.read() 'This is the entire file.\n' >>> f.read() '' -``f.readline()`` reads a single line from the file; a newline character (``\n``) -is left at the end of the string, and is only omitted on the last line of the -file if the file doesn't end in a newline. This makes the return value -unambiguous; if ``f.readline()`` returns an empty string, the end of the file -has been reached, while a blank line is represented by ``'\n'``, a string -containing only a single newline. :: +:meth:`f.readline() ` reads a single line from the file; a +newline character (``\n``) is left at the end of the string, and is only +omitted on the last line of the file if the file doesn't end in a newline. This +makes the return value unambiguous; if ``f.readline()`` returns an empty string, +the end of the file has been reached, while a blank line is represented by +``'\n'``, a string containing only a single newline. :: >>> f.readline() 'This is the first line of the file.\n' @@ -433,10 +433,10 @@ efficient, fast, and leads to simple code:: Second line of the file If you want to read all the lines of a file in a list you can also use -``list(f)`` or ``f.readlines()``. +``list(f)`` or :meth:`f.readlines() `. -``f.write(string)`` writes the contents of *string* to the file, returning -the number of characters written. :: +:meth:`f.write(string) ` writes the contents of *string* to +the file, returning the number of characters written. :: >>> f.write('This is a test\n') 15 @@ -449,15 +449,16 @@ or a bytes object (in binary mode) -- before writing them:: >>> f.write(s) 18 -``f.tell()`` returns an integer giving the file object's current position in the file -represented as number of bytes from the beginning of the file when in binary mode and -an opaque number when in text mode. +:meth:`f.tell() ` returns an integer giving the file object's +current position in the file represented as number of bytes from the beginning +of the file when in binary mode and an opaque number when in text mode. -To change the file object's position, use ``f.seek(offset, whence)``. The position is computed +To change the file object's position, use +:meth:`f.seek(offset, whence) `. The position is computed from adding *offset* to a reference point; the reference point is selected by the *whence* argument. A *whence* value of 0 measures from the beginning -of the file, 1 uses the current file position, and 2 uses the end of the file as -the reference point. *whence* can be omitted and defaults to 0, using the +of the file, 1 uses the current file position, and 2 uses the end of the file +as the reference point. *whence* can be omitted and defaults to 0, using the beginning of the file as the reference point. :: >>> f = open('workfile', 'rb+') From 2b06df4eaf6bcf286f96ce5ce1595e7d51004f89 Mon Sep 17 00:00:00 2001 From: Bonu Krishna Chaitanya Date: Sat, 13 Sep 2025 12:49:29 +0530 Subject: [PATCH 2/2] format --- Doc/tutorial/inputoutput.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst index 7d23293b0a28ef..6956995e1ea779 100644 --- a/Doc/tutorial/inputoutput.rst +++ b/Doc/tutorial/inputoutput.rst @@ -454,11 +454,11 @@ current position in the file represented as number of bytes from the beginning of the file when in binary mode and an opaque number when in text mode. To change the file object's position, use -:meth:`f.seek(offset, whence) `. The position is computed -from adding *offset* to a reference point; the reference point is selected by -the *whence* argument. A *whence* value of 0 measures from the beginning -of the file, 1 uses the current file position, and 2 uses the end of the file -as the reference point. *whence* can be omitted and defaults to 0, using the +:meth:`f.seek(offset, whence) `. The position is computed from +adding *offset* to a reference point; the reference point is selected by the +*whence* argument. A *whence* value of 0 measures from the beginning of the +file, 1 uses the current file position, and 2 uses the end of the file as the +reference point. *whence* can be omitted and defaults to 0, using the beginning of the file as the reference point. :: >>> f = open('workfile', 'rb+')