@@ -395,26 +395,26 @@ Methods of File Objects
395
395
The rest of the examples in this section will assume that a file object called
396
396
``f `` has already been created.
397
397
398
- To read a file's contents, call `` f.read(size) `` , which reads some quantity of
399
- data and returns it as a string (in text mode) or bytes object (in binary mode).
400
- *size * is an optional numeric argument. When *size * is omitted or negative, the
401
- entire contents of the file will be read and returned; it's your problem if the
402
- file is twice as large as your machine's memory. Otherwise, at most * size *
403
- characters (in text mode) or *size * bytes (in binary mode) are read and returned.
404
- If the end of the file has been reached, `` f.read() `` will return an empty
405
- string (``'' ``). ::
398
+ To read a file's contents, call :meth: ` f.read(size) <io.RawIOBase.read> ` , which
399
+ reads some quantity of data and returns it as a string (in text mode) or bytes
400
+ object (in binary mode). *size * is an optional numeric argument. When *size * is
401
+ omitted or negative, the entire contents of the file will be read and returned;
402
+ it's your problem if the file is twice as large as your machine's memory.
403
+ Otherwise, at most *size * characters (in text mode) or * size * bytes
404
+ (in binary mode) are read and returned. If the end of the file has been reached,
405
+ `` f.read() `` will return an empty string (``'' ``). ::
406
406
407
407
>>> f.read()
408
408
'This is the entire file.\n'
409
409
>>> f.read()
410
410
''
411
411
412
- `` f.readline() `` reads a single line from the file; a newline character (`` \n ``)
413
- is left at the end of the string, and is only omitted on the last line of the
414
- file if the file doesn't end in a newline. This makes the return value
415
- unambiguous; if ``f.readline() `` returns an empty string, the end of the file
416
- has been reached, while a blank line is represented by `` '\n' ``, a string
417
- containing only a single newline. ::
412
+ :meth: ` f.readline() <io.IOBase.readline> ` reads a single line from the file; a
413
+ newline character (`` \n ``) is left at the end of the string, and is only
414
+ omitted on the last line of the file if the file doesn't end in a newline. This
415
+ makes the return value unambiguous; if ``f.readline() `` returns an empty string,
416
+ the end of the file has been reached, while a blank line is represented by
417
+ `` '\n' ``, a string containing only a single newline. ::
418
418
419
419
>>> f.readline()
420
420
'This is the first line of the file.\n'
@@ -433,10 +433,10 @@ efficient, fast, and leads to simple code::
433
433
Second line of the file
434
434
435
435
If you want to read all the lines of a file in a list you can also use
436
- ``list(f) `` or `` f.readlines() ` `.
436
+ ``list(f) `` or :meth: ` f.readlines() <io.IOBase.readlines> `.
437
437
438
- `` f.write(string) `` writes the contents of *string * to the file, returning
439
- the number of characters written. ::
438
+ :meth: ` f.write(string) <io.RawIOBase.write> ` writes the contents of *string * to
439
+ the file, returning the number of characters written. ::
440
440
441
441
>>> f.write('This is a test\n')
442
442
15
@@ -449,15 +449,16 @@ or a bytes object (in binary mode) -- before writing them::
449
449
>>> f.write(s)
450
450
18
451
451
452
- `` f.tell() `` returns an integer giving the file object's current position in the file
453
- represented as number of bytes from the beginning of the file when in binary mode and
454
- an opaque number when in text mode.
452
+ :meth: ` f.tell() <io.IOBase.tell> ` returns an integer giving the file object's
453
+ current position in the file represented as number of bytes from the beginning
454
+ of the file when in binary mode and an opaque number when in text mode.
455
455
456
- To change the file object's position, use ``f.seek(offset, whence) ``. The position is computed
456
+ To change the file object's position, use
457
+ :meth: `f.seek(offset, whence) <io.IOBase.seek> `. The position is computed
457
458
from adding *offset * to a reference point; the reference point is selected by
458
459
the *whence * argument. A *whence * value of 0 measures from the beginning
459
- of the file, 1 uses the current file position, and 2 uses the end of the file as
460
- the reference point. *whence * can be omitted and defaults to 0, using the
460
+ of the file, 1 uses the current file position, and 2 uses the end of the file
461
+ as the reference point. *whence * can be omitted and defaults to 0, using the
461
462
beginning of the file as the reference point. ::
462
463
463
464
>>> f = open('workfile', 'rb+')
0 commit comments