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

Skip to content

Commit b1a1810

Browse files
committed
Merged revisions 76893 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ........ r76893 | antoine.pitrou | 2009-12-19 19:22:15 +0100 (sam., 19 déc. 2009) | 4 lines Issue #7508: remove obsolete documentation about built-in file objects. ........
1 parent 2297b09 commit b1a1810

1 file changed

Lines changed: 2 additions & 285 deletions

File tree

Doc/library/stdtypes.rst

Lines changed: 2 additions & 285 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interpreter.
1212

1313
.. index:: pair: built-in; types
1414

15-
The principal built-in types are numerics, sequences, mappings, files, classes,
15+
The principal built-in types are numerics, sequences, mappings, classes,
1616
instances and exceptions.
1717

1818
Some operations are supported by several object types; in particular,
@@ -165,7 +165,7 @@ This table summarizes the comparison operations:
165165
pair: objects; comparing
166166

167167
Objects of different types, except different numeric types, never compare equal.
168-
Furthermore, some types (for example, file objects) support only a degenerate
168+
Furthermore, some types (for example, function objects) support only a degenerate
169169
notion of comparison where any two objects of that type are unequal. The ``<``,
170170
``<=``, ``>`` and ``>=`` operators will raise a :exc:`TypeError` exception when
171171
any operand is a complex number, the objects are of different types that cannot
@@ -2066,283 +2066,6 @@ An example of dictionary view usage::
20662066
{'bacon'}
20672067

20682068

2069-
.. _bltin-file-objects:
2070-
2071-
File Objects
2072-
============
2073-
2074-
.. index::
2075-
object: file
2076-
builtin: file
2077-
module: os
2078-
module: socket
2079-
2080-
.. XXX this is quite out of date, must be updated with "io" module
2081-
2082-
File objects are implemented using C's ``stdio`` package and can be
2083-
created with the built-in :func:`open` function. File
2084-
objects are also returned by some other built-in functions and methods,
2085-
such as :func:`os.popen` and :func:`os.fdopen` and the :meth:`makefile`
2086-
method of socket objects. Temporary files can be created using the
2087-
:mod:`tempfile` module, and high-level file operations such as copying,
2088-
moving, and deleting files and directories can be achieved with the
2089-
:mod:`shutil` module.
2090-
2091-
When a file operation fails for an I/O-related reason, the exception
2092-
:exc:`IOError` is raised. This includes situations where the operation is not
2093-
defined for some reason, like :meth:`seek` on a tty device or writing a file
2094-
opened for reading.
2095-
2096-
Files have the following methods:
2097-
2098-
2099-
.. method:: file.close()
2100-
2101-
Close the file. A closed file cannot be read or written any more. Any operation
2102-
which requires that the file be open will raise a :exc:`ValueError` after the
2103-
file has been closed. Calling :meth:`close` more than once is allowed.
2104-
2105-
You can avoid having to call this method explicitly if you use
2106-
the :keyword:`with` statement. For example, the following code will
2107-
automatically close *f* when the :keyword:`with` block is exited::
2108-
2109-
from __future__ import with_statement # This isn't required in Python 2.6
2110-
2111-
with open("hello.txt") as f:
2112-
for line in f:
2113-
print(line)
2114-
2115-
In older versions of Python, you would have needed to do this to get the same
2116-
effect::
2117-
2118-
f = open("hello.txt")
2119-
try:
2120-
for line in f:
2121-
print(line)
2122-
finally:
2123-
f.close()
2124-
2125-
.. note::
2126-
2127-
Not all "file-like" types in Python support use as a context manager for the
2128-
:keyword:`with` statement. If your code is intended to work with any file-like
2129-
object, you can use the function :func:`contextlib.closing` instead of using
2130-
the object directly.
2131-
2132-
2133-
.. method:: file.flush()
2134-
2135-
Flush the internal buffer, like ``stdio``'s :cfunc:`fflush`. This may be a
2136-
no-op on some file-like objects.
2137-
2138-
.. note::
2139-
2140-
:meth:`flush` does not necessarily write the file's data to disk. Use
2141-
:meth:`flush` followed by :func:`os.fsync` to ensure this behavior.
2142-
2143-
2144-
.. method:: file.fileno()
2145-
2146-
.. index::
2147-
pair: file; descriptor
2148-
module: fcntl
2149-
2150-
Return the integer "file descriptor" that is used by the underlying
2151-
implementation to request I/O operations from the operating system. This can be
2152-
useful for other, lower level interfaces that use file descriptors, such as the
2153-
:mod:`fcntl` module or :func:`os.read` and friends.
2154-
2155-
.. note::
2156-
2157-
File-like objects which do not have a real file descriptor should *not* provide
2158-
this method!
2159-
2160-
2161-
.. method:: file.isatty()
2162-
2163-
Return ``True`` if the file is connected to a tty(-like) device, else ``False``.
2164-
2165-
.. note::
2166-
2167-
If a file-like object is not associated with a real file, this method should
2168-
*not* be implemented.
2169-
2170-
2171-
.. method:: file.__next__()
2172-
2173-
A file object is its own iterator, for example ``iter(f)`` returns *f* (unless
2174-
*f* is closed). When a file is used as an iterator, typically in a
2175-
:keyword:`for` loop (for example, ``for line in f: print(line)``), the
2176-
:meth:`__next__` method is called repeatedly. This method returns the next
2177-
input line, or raises :exc:`StopIteration` when EOF is hit when the file is open
2178-
for reading (behavior is undefined when the file is open for writing). In order
2179-
to make a :keyword:`for` loop the most efficient way of looping over the lines
2180-
of a file (a very common operation), the :meth:`__next__` method uses a hidden
2181-
read-ahead buffer. As a consequence of using a read-ahead buffer, combining
2182-
:meth:`__next__` with other file methods (like :meth:`readline`) does not work
2183-
right. However, using :meth:`seek` to reposition the file to an absolute
2184-
position will flush the read-ahead buffer.
2185-
2186-
2187-
.. method:: file.read([size])
2188-
2189-
Read at most *size* bytes from the file (less if the read hits EOF before
2190-
obtaining *size* bytes). If the *size* argument is negative or omitted, read
2191-
all data until EOF is reached. The bytes are returned as a string object. An
2192-
empty string is returned when EOF is encountered immediately. (For certain
2193-
files, like ttys, it makes sense to continue reading after an EOF is hit.) Note
2194-
that this method may call the underlying C function :cfunc:`fread` more than
2195-
once in an effort to acquire as close to *size* bytes as possible. Also note
2196-
that when in non-blocking mode, less data than was requested may be
2197-
returned, even if no *size* parameter was given.
2198-
2199-
2200-
.. method:: file.readline([size])
2201-
2202-
Read one entire line from the file. A trailing newline character is kept in the
2203-
string (but may be absent when a file ends with an incomplete line). [#]_ If
2204-
the *size* argument is present and non-negative, it is a maximum byte count
2205-
(including the trailing newline) and an incomplete line may be returned. An
2206-
empty string is returned *only* when EOF is encountered immediately.
2207-
2208-
.. note::
2209-
2210-
Unlike ``stdio``'s :cfunc:`fgets`, the returned string contains null characters
2211-
(``'\0'``) if they occurred in the input.
2212-
2213-
2214-
.. method:: file.readlines([sizehint])
2215-
2216-
Read until EOF using :meth:`readline` and return a list containing the lines
2217-
thus read. If the optional *sizehint* argument is present, instead of
2218-
reading up to EOF, whole lines totalling approximately *sizehint* bytes
2219-
(possibly after rounding up to an internal buffer size) are read. Objects
2220-
implementing a file-like interface may choose to ignore *sizehint* if it
2221-
cannot be implemented, or cannot be implemented efficiently.
2222-
2223-
2224-
.. method:: file.seek(offset[, whence])
2225-
2226-
Set the file's current position, like ``stdio``'s :cfunc:`fseek`. The *whence*
2227-
argument is optional and defaults to ``os.SEEK_SET`` or ``0`` (absolute file
2228-
positioning); other values are ``os.SEEK_CUR`` or ``1`` (seek relative to the
2229-
current position) and ``os.SEEK_END`` or ``2`` (seek relative to the file's
2230-
end). There is no return value.
2231-
2232-
For example, ``f.seek(2, os.SEEK_CUR)`` advances the position by two and
2233-
``f.seek(-3, os.SEEK_END)`` sets the position to the third to last.
2234-
2235-
Note that if the file is opened for appending
2236-
(mode ``'a'`` or ``'a+'``), any :meth:`seek` operations will be undone at the
2237-
next write. If the file is only opened for writing in append mode (mode
2238-
``'a'``), this method is essentially a no-op, but it remains useful for files
2239-
opened in append mode with reading enabled (mode ``'a+'``). If the file is
2240-
opened in text mode (without ``'b'``), only offsets returned by :meth:`tell` are
2241-
legal. Use of other offsets causes undefined behavior.
2242-
2243-
Note that not all file objects are seekable.
2244-
2245-
2246-
.. method:: file.tell()
2247-
2248-
Return the file's current position, like ``stdio``'s :cfunc:`ftell`.
2249-
2250-
.. note::
2251-
2252-
On Windows, :meth:`tell` can return illegal values (after an :cfunc:`fgets`)
2253-
when reading files with Unix-style line-endings. Use binary mode (``'rb'``) to
2254-
circumvent this problem.
2255-
2256-
2257-
.. method:: file.truncate([size])
2258-
2259-
Truncate the file's size. If the optional *size* argument is present, the file
2260-
is truncated to (at most) that size. The size defaults to the current position.
2261-
The current file position is not changed. Note that if a specified size exceeds
2262-
the file's current size, the result is platform-dependent: possibilities
2263-
include that the file may remain unchanged, increase to the specified size as if
2264-
zero-filled, or increase to the specified size with undefined new content.
2265-
Availability: Windows, many Unix variants.
2266-
2267-
2268-
.. method:: file.write(str)
2269-
2270-
Write a string to the file. Due to buffering, the string may not actually
2271-
show up in the file until the :meth:`flush` or :meth:`close` method is
2272-
called.
2273-
2274-
The meaning of the return value is not defined for every file-like object.
2275-
Some (mostly low-level) file-like objects may return the number of bytes
2276-
actually written, others return ``None``.
2277-
2278-
2279-
.. method:: file.writelines(sequence)
2280-
2281-
Write a sequence of strings to the file. The sequence can be any iterable
2282-
object producing strings, typically a list of strings. There is no return value.
2283-
(The name is intended to match :meth:`readlines`; :meth:`writelines` does not
2284-
add line separators.)
2285-
2286-
Files support the iterator protocol. Each iteration returns the same result as
2287-
``file.readline()``, and iteration ends when the :meth:`readline` method returns
2288-
an empty string.
2289-
2290-
File objects also offer a number of other interesting attributes. These are not
2291-
required for file-like objects, but should be implemented if they make sense for
2292-
the particular object.
2293-
2294-
2295-
.. attribute:: file.closed
2296-
2297-
bool indicating the current state of the file object. This is a read-only
2298-
attribute; the :meth:`close` method changes the value. It may not be available
2299-
on all file-like objects.
2300-
2301-
2302-
.. XXX does this still apply?
2303-
.. attribute:: file.encoding
2304-
2305-
The encoding that this file uses. When strings are written to a file,
2306-
they will be converted to byte strings using this encoding. In addition, when
2307-
the file is connected to a terminal, the attribute gives the encoding that the
2308-
terminal is likely to use (that information might be incorrect if the user has
2309-
misconfigured the terminal). The attribute is read-only and may not be present
2310-
on all file-like objects. It may also be ``None``, in which case the file uses
2311-
the system default encoding for converting strings.
2312-
2313-
2314-
.. attribute:: file.errors
2315-
2316-
The Unicode error handler used along with the encoding.
2317-
2318-
2319-
.. attribute:: file.mode
2320-
2321-
The I/O mode for the file. If the file was created using the :func:`open`
2322-
built-in function, this will be the value of the *mode* parameter. This is a
2323-
read-only attribute and may not be present on all file-like objects.
2324-
2325-
2326-
.. attribute:: file.name
2327-
2328-
If the file object was created using :func:`open`, the name of the file.
2329-
Otherwise, some string that indicates the source of the file object, of the
2330-
form ``<...>``. This is a read-only attribute and may not be present on all
2331-
file-like objects.
2332-
2333-
2334-
.. attribute:: file.newlines
2335-
2336-
If Python was built with the :option:`--with-universal-newlines` option to
2337-
:program:`configure` (the default) this read-only attribute exists, and for
2338-
files opened in universal newline read mode it keeps track of the types of
2339-
newlines encountered while reading the file. The values it can take are
2340-
``'\r'``, ``'\n'``, ``'\r\n'``, ``None`` (unknown, no newlines read yet) or a
2341-
tuple containing all the newline types seen, to indicate that multiple newline
2342-
conventions were encountered. For files not opened in universal newline read
2343-
mode the value of this attribute will be ``None``.
2344-
2345-
23462069
.. _typememoryview:
23472070

23482071
memoryview Types
@@ -2777,9 +2500,3 @@ The following attributes are only supported by :term:`new-style class`\ es.
27772500
27782501
.. [#] To format only a tuple you should therefore provide a singleton tuple whose only
27792502
element is the tuple to be formatted.
2780-
2781-
.. [#] The advantage of leaving the newline on is that returning an empty string is
2782-
then an unambiguous EOF indication. It is also possible (in cases where it
2783-
might matter, for example, if you want to make an exact copy of a file while
2784-
scanning its lines) to tell whether the last line of a file ended in a newline
2785-
or not (yes this happens!).

0 commit comments

Comments
 (0)