mirror of
https://github.com/python/cpython.git
synced 2025-11-03 19:34:08 +00:00
#3524: fix up some old-style IO descriptions.
This commit is contained in:
parent
f5f2630db6
commit
0dcb7ac754
1 changed files with 37 additions and 26 deletions
|
|
@ -228,6 +228,9 @@ arguments: ``open(filename, mode)``.
|
||||||
::
|
::
|
||||||
|
|
||||||
>>> f = open('/tmp/workfile', 'w')
|
>>> f = open('/tmp/workfile', 'w')
|
||||||
|
|
||||||
|
.. XXX str(f) is <io.TextIOWrapper object at 0x82e8dc4>
|
||||||
|
|
||||||
>>> print(f)
|
>>> print(f)
|
||||||
<open file '/tmp/workfile', mode 'w' at 80a0960>
|
<open file '/tmp/workfile', mode 'w' at 80a0960>
|
||||||
|
|
||||||
|
|
@ -240,19 +243,18 @@ automatically added to the end. ``'r+'`` opens the file for both reading and
|
||||||
writing. The *mode* argument is optional; ``'r'`` will be assumed if it's
|
writing. The *mode* argument is optional; ``'r'`` will be assumed if it's
|
||||||
omitted.
|
omitted.
|
||||||
|
|
||||||
On Windows and the Macintosh, ``'b'`` appended to the mode opens the file in
|
Normally, files are opened in :dfn:`text mode`, that means, you read and write
|
||||||
binary mode, so there are also modes like ``'rb'``, ``'wb'``, and ``'r+b'``.
|
strings from and to the file, which are encoded in a specific encoding (the
|
||||||
Windows makes a distinction between text and binary files; the end-of-line
|
default being UTF-8). ``'b'`` appended to the mode opens the file in
|
||||||
characters in text files are automatically altered slightly when data is read or
|
:dfn:`binary mode`: now the data is read and written in the form of bytes
|
||||||
written. This behind-the-scenes modification to file data is fine for ASCII
|
objects. This mode should be used for all files that don't contain text.
|
||||||
text files, but it'll corrupt binary data like that in :file:`JPEG` or
|
|
||||||
:file:`EXE` files. Be very careful to use binary mode when reading and writing
|
|
||||||
such files. On Unix, it doesn't hurt to append a ``'b'`` to the mode, so
|
|
||||||
you can use it platform-independently for all binary files.
|
|
||||||
|
|
||||||
This behind-the-scenes modification to file data is fine for text files, but
|
In text mode, the default is to convert platform-specific line endings (``\n``
|
||||||
will corrupt binary data like that in :file:`JPEG` or :file:`EXE` files. Be
|
on Unix, ``\r\n`` on Windows) to just ``\n`` on reading and ``\n`` back to
|
||||||
very careful to use binary mode when reading and writing such files.
|
platform-specific line endings on writing. This behind-the-scenes modification
|
||||||
|
to file data is fine for text files, but will corrupt binary data like that in
|
||||||
|
:file:`JPEG` or :file:`EXE` files. Be very careful to use binary mode when
|
||||||
|
reading and writing such files.
|
||||||
|
|
||||||
|
|
||||||
.. _tut-filemethods:
|
.. _tut-filemethods:
|
||||||
|
|
@ -264,12 +266,12 @@ The rest of the examples in this section will assume that a file object called
|
||||||
``f`` has already been created.
|
``f`` has already been created.
|
||||||
|
|
||||||
To read a file's contents, call ``f.read(size)``, which reads some quantity of
|
To read a file's contents, call ``f.read(size)``, which reads some quantity of
|
||||||
data and returns it as a string. *size* is an optional numeric argument. When
|
data and returns it as a string or bytes object. *size* is an optional numeric
|
||||||
*size* is omitted or negative, the entire contents of the file will be read and
|
argument. When *size* is omitted or negative, the entire contents of the file
|
||||||
returned; it's your problem if the file is twice as large as your machine's
|
will be read and returned; it's your problem if the file is twice as large as
|
||||||
memory. Otherwise, at most *size* bytes are read and returned. If the end of
|
your machine's memory. Otherwise, at most *size* bytes are read and returned.
|
||||||
the file has been reached, ``f.read()`` will return an empty string (``""``).
|
If the end of the file has been reached, ``f.read()`` will return an empty
|
||||||
::
|
string (``''``). ::
|
||||||
|
|
||||||
>>> f.read()
|
>>> f.read()
|
||||||
'This is the entire file.\n'
|
'This is the entire file.\n'
|
||||||
|
|
@ -281,7 +283,7 @@ 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
|
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
|
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
|
has been reached, while a blank line is represented by ``'\n'``, a string
|
||||||
containing only a single newline. ::
|
containing only a single newline. ::
|
||||||
|
|
||||||
>>> f.readline()
|
>>> f.readline()
|
||||||
'This is the first line of the file.\n'
|
'This is the first line of the file.\n'
|
||||||
|
|
@ -304,8 +306,8 @@ An alternative approach to reading lines is to loop over the file object. This i
|
||||||
memory efficient, fast, and leads to simpler code::
|
memory efficient, fast, and leads to simpler code::
|
||||||
|
|
||||||
>>> for line in f:
|
>>> for line in f:
|
||||||
print(line, end='')
|
... print(line, end='')
|
||||||
|
...
|
||||||
This is the first line of the file.
|
This is the first line of the file.
|
||||||
Second line of the file
|
Second line of the file
|
||||||
|
|
||||||
|
|
@ -314,9 +316,10 @@ control. Since the two approaches manage line buffering differently, they
|
||||||
should not be mixed.
|
should not be mixed.
|
||||||
|
|
||||||
``f.write(string)`` writes the contents of *string* to the file, returning
|
``f.write(string)`` writes the contents of *string* to the file, returning
|
||||||
``None``. ::
|
the number of characters written. ::
|
||||||
|
|
||||||
>>> f.write('This is a test\n')
|
>>> f.write('This is a test\n')
|
||||||
|
15
|
||||||
|
|
||||||
To write something other than a string, it needs to be converted to a string
|
To write something other than a string, it needs to be converted to a string
|
||||||
first::
|
first::
|
||||||
|
|
@ -324,6 +327,7 @@ first::
|
||||||
>>> value = ('the answer', 42)
|
>>> value = ('the answer', 42)
|
||||||
>>> s = str(value)
|
>>> s = str(value)
|
||||||
>>> f.write(s)
|
>>> f.write(s)
|
||||||
|
18
|
||||||
|
|
||||||
``f.tell()`` returns an integer giving the file object's current position in the
|
``f.tell()`` returns an integer giving the file object's current position in the
|
||||||
file, measured in bytes from the beginning of the file. To change the file
|
file, measured in bytes from the beginning of the file. To change the file
|
||||||
|
|
@ -334,15 +338,22 @@ of the file, 1 uses the current file position, and 2 uses the end of the file as
|
||||||
the reference point. *from_what* can be omitted and defaults to 0, using the
|
the reference point. *from_what* can be omitted and defaults to 0, using the
|
||||||
beginning of the file as the reference point. ::
|
beginning of the file as the reference point. ::
|
||||||
|
|
||||||
>>> f = open('/tmp/workfile', 'r+')
|
>>> f = open('/tmp/workfile', 'rb+')
|
||||||
>>> f.write('0123456789abcdef')
|
>>> f.write(b'0123456789abcdef')
|
||||||
|
16
|
||||||
>>> f.seek(5) # Go to the 6th byte in the file
|
>>> f.seek(5) # Go to the 6th byte in the file
|
||||||
|
5
|
||||||
>>> f.read(1)
|
>>> f.read(1)
|
||||||
'5'
|
b'5'
|
||||||
>>> f.seek(-3, 2) # Go to the 3rd byte before the end
|
>>> f.seek(-3, 2) # Go to the 3rd byte before the end
|
||||||
|
13
|
||||||
>>> f.read(1)
|
>>> f.read(1)
|
||||||
'd'
|
b'd'
|
||||||
|
|
||||||
|
In text files (those opened without a ``b`` in the mode string), only seeks
|
||||||
|
relative to the beginning of the file are allowed (the exception being seeking
|
||||||
|
to the very file end with ``seek(0, 2)``).
|
||||||
|
|
||||||
When you're done with a file, call ``f.close()`` to close it and free up any
|
When you're done with a file, call ``f.close()`` to close it and free up any
|
||||||
system resources taken up by the open file. After calling ``f.close()``,
|
system resources taken up by the open file. After calling ``f.close()``,
|
||||||
attempts to use the file object will automatically fail. ::
|
attempts to use the file object will automatically fail. ::
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue