blob: a34c09aab322534ea986691816601b7cd93170b0 [file] [log] [blame]
Georg Brandl014197c2008-04-09 18:40:51 +00001:mod:`io` --- Core tools for working with streams
2=================================================
3
4.. module:: io
5 :synopsis: Core tools for working with streams.
6.. moduleauthor:: Guido van Rossum <guido@python.org>
7.. moduleauthor:: Mike Verdone <mike.verdone@gmail.com>
8.. moduleauthor:: Mark Russell <mark.russell@zen.co.uk>
9.. sectionauthor:: Benjamin Peterson
10
11The :mod:`io` module provides the Python interfaces to stream handling. The
12builtin :func:`open` function is defined in this module.
13
14At the top of the I/O hierarchy is the abstract base class :class:`IOBase`. It
15defines the basic interface to a stream. Note, however, that there is no
16seperation between reading and writing to streams; implementations are allowed
17to throw an :exc:`IOError` if they do not support a given operation.
18
19Extending :class:`IOBase` is :class:`RawIOBase` which deals simply with the
20reading and writing of raw bytes to a stream. :class:`FileIO` subclasses
21:class:`RawIOBase` to provide an interface to OS files.
22
23:class:`BufferedIOBase` deals with buffering on a raw byte stream
24(:class:`RawIOBase`). Its subclasses, :class:`BufferedWriter`,
25:class:`BufferedReader`, and :class:`BufferedRWPair` buffer streams that are
26readable, writable, and both respectively. :class:`BufferedRandom` provides a
27buffered interface to random access streams. :class:`BytesIO` is a simple
28stream of in-memory bytes.
29
Benjamin Peterson2c5f8282008-04-13 00:27:46 +000030Another :class:`IOBase` subclass, :class:`TextIOBase`, deals with the encoding
Georg Brandl014197c2008-04-09 18:40:51 +000031and decoding of streams into text. :class:`TextIOWrapper`, which extends it, is
32a buffered text interface to a buffered raw stream (:class:`BufferedIOBase`).
33Finally, :class:`StringIO` is a in-memory stream for text.
34
Benjamin Peterson2c5f8282008-04-13 00:27:46 +000035Argument names are not part of the specification, and only the arguments of
36:func:`open()` are intended to be used as keyword arguments.
37
Georg Brandl014197c2008-04-09 18:40:51 +000038
39Module Interface
40----------------
41
42.. data:: DEFAULT_BUFFER_SIZE
43
44 An int containing the default buffer size used by the module's buffered I/O
Benjamin Peterson2c5f8282008-04-13 00:27:46 +000045 classes. :func:`open()` uses the file's blksize (as obtained by
46 :func:`os.stat`) if possible.
Georg Brandl014197c2008-04-09 18:40:51 +000047
48.. function:: open(file[, mode[, buffering[, encoding[, errors[, newline[, closefd=True]]]]]])
49
Benjamin Petersondd219122008-04-11 21:17:32 +000050 Open *file* and return a stream. If the file cannot be opened, an
51 :exc:`IOError` is raised.
Georg Brandl014197c2008-04-09 18:40:51 +000052
Benjamin Petersondd219122008-04-11 21:17:32 +000053 *file* is either a string giving the name (and the path if the file isn't in
54 the current working directory) of the file to be opened or an integer file
55 descriptor of the file to be wrapped. (If a file descriptor is given, it is
56 closed when the returned I/O object is closed, unless *closefd* is set to
57 ``False``.)
Georg Brandl014197c2008-04-09 18:40:51 +000058
Benjamin Petersondd219122008-04-11 21:17:32 +000059 *mode* is an optional string that specifies the mode in which the file is
60 opened. It defaults to ``'r'`` which means open for reading in text mode.
61 Other common values are ``'w'`` for writing (truncating the file if it
62 already exists), and ``'a'`` for appending (which on *some* Unix systems,
63 means that *all* writes append to the end of the file regardless of the
64 current seek position). In text mode, if *encoding* is not specified the
65 encoding used is platform dependent. (For reading and writing raw bytes use
66 binary mode and leave *encoding* unspecified.) The available modes are:
Georg Brandl014197c2008-04-09 18:40:51 +000067
68 ========= ===============================================================
69 Character Meaning
70 --------- ---------------------------------------------------------------
71 ``'r'`` open for reading (default)
72 ``'w'`` open for writing, truncating the file first
73 ``'a'`` open for writing, appending to the end of the file if it exists
74 ``'b'`` binary mode
75 ``'t'`` text mode (default)
76 ``'+'`` open a disk file for updating (reading and writing)
77 ``'U'`` universal newline mode (for backwards compatibility; unneeded
78 for new code)
79 ========= ===============================================================
80
81 The default mode is ``'rt'`` (open for reading text). For binary random
82 access, the mode ``'w+b'`` opens and truncates the file to 0 bytes, while
83 ``'r+b'`` opens the file without truncation.
84
Benjamin Peterson2c5f8282008-04-13 00:27:46 +000085 Python distinguishes between files opened in binary and text modes, even when
86 the underlying operating system doesn't. Files opened in binary mode
87 (appending ``'b'`` to the *mode* argument) return contents as ``bytes``
88 objects without any decoding. In text mode (the default, or when ``'t'`` is
89 appended to the *mode* argument), the contents of the file are returned as
90 strings, the bytes having been first decoded using a platform-dependent
91 encoding or using the specified *encoding* if given.
Benjamin Petersondd219122008-04-11 21:17:32 +000092
93 *buffering* is an optional integer used to set the buffering policy. By
Benjamin Peterson2c5f8282008-04-13 00:27:46 +000094 default full buffering is on. Pass 0 to switch buffering off (only allowed
95 in binary mode), 1 to set line buffering, and an integer > 1 for full
96 buffering.
Georg Brandl014197c2008-04-09 18:40:51 +000097
98 *encoding* is the name of the encoding used to decode or encode the file.
Benjamin Petersondd219122008-04-11 21:17:32 +000099 This should only be used in text mode. The default encoding is platform
100 dependent, but any encoding supported by Python can be passed. See the
101 :mod:`codecs` module for the list of supported encodings.
Georg Brandl014197c2008-04-09 18:40:51 +0000102
Benjamin Petersondd219122008-04-11 21:17:32 +0000103 *errors* is an optional string that specifies how encoding errors are to be
104 handled---this argument should not be used in binary mode. Pass ``'strict'``
105 to raise a :exc:`ValueError` exception if there is an encoding error (the
106 default of ``None`` has the same effect), or pass ``'ignore'`` to ignore
107 errors. (Note that ignoring encoding errors can lead to data loss.) See the
108 documentation for :func:`codecs.register` for a list of the permitted
109 encoding error strings.
Georg Brandl014197c2008-04-09 18:40:51 +0000110
111 *newline* controls how universal newlines works (it only applies to text
112 mode). It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It
113 works as follows:
114
115 * On input, if *newline* is ``None``, universal newlines mode is enabled.
116 Lines in the input can end in ``'\n'``, ``'\r'``, or ``'\r\n'``, and these
117 are translated into ``'\n'`` before being returned to the caller. If it is
118 ``''``, universal newline mode is enabled, but line endings are returned to
119 the caller untranslated. If it has any of the other legal values, input
120 lines are only terminated by the given string, and the line ending is
121 returned to the caller untranslated.
122
123 * On output, if *newline* is ``None``, any ``'\n'`` characters written are
124 translated to the system default line separator, :data:`os.linesep`. If
125 *newline* is ``''``, no translation takes place. If *newline* is any of
126 the other legal values, any ``'\n'`` characters written are translated to
127 the given string.
128
Benjamin Petersondd219122008-04-11 21:17:32 +0000129 If *closefd* is ``False``, the underlying file descriptor will be kept open
130 when the file is closed. This does not work when a file name is given and
131 must be ``True`` in that case.
Georg Brandl014197c2008-04-09 18:40:51 +0000132
Benjamin Petersondd219122008-04-11 21:17:32 +0000133 :func:`open()` returns a file object whose type depends on the mode, and
134 through which the standard file operations such as reading and writing are
135 performed. When :func:`open()` is used to open a file in a text mode
136 (``'w'``, ``'r'``, ``'wt'``, ``'rt'``, etc.), it returns a
Georg Brandl014197c2008-04-09 18:40:51 +0000137 :class:`TextIOWrapper`. When used to open a file in a binary mode, the
138 returned class varies: in read binary mode, it returns a
139 :class:`BufferedReader`; in write binary and append binary modes, it returns
140 a :class:`BufferedWriter`, and in read/write mode, it returns a
141 :class:`BufferedRandom`.
142
143 It is also possible to use a string or bytearray as a file for both reading
Benjamin Petersondd219122008-04-11 21:17:32 +0000144 and writing. For strings :class:`StringIO` can be used like a file opened in
145 a text mode, and for bytes a :class:`BytesIO` can be used like a file opened
146 in a binary mode.
Georg Brandl014197c2008-04-09 18:40:51 +0000147
148
149.. exception:: BlockingIOError
150
151 Error raised when blocking would occur on a non-blocking stream. It inherits
152 :exc:`IOError`.
153
154 In addition to those of :exc:`IOError`, :exc:`BlockingIOError` has one
155 attribute:
156
157 .. attribute:: characters_written
158
159 An integer containing the number of characters written to the stream
160 before it blocked.
161
162
163.. exception:: UnsupportedOperation
164
165 An exception inheriting :exc:`IOError` and :exc:`ValueError` that is raised
166 when an unsupported operation is called on a stream.
167
168
169I/O Base Classes
170----------------
171
172.. class:: IOBase
173
174 The abstract base class for all I/O classes, acting on streams of bytes.
175 There is no public constructor.
176
177 This class provides dummy implementations for many methods that derived
178 classes can override selectively; the default implementations represent a
179 file that cannot be read, written or seeked.
180
181 Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`,
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000182 or :meth:`write` because their signatures will vary, implementations and
183 clients should consider those methods part of the interface. Also,
184 implementations may raise a :exc:`IOError` when operations they do not
185 support are called.
Georg Brandl014197c2008-04-09 18:40:51 +0000186
187 The basic type used for binary data read from or written to a file is
188 :class:`bytes`. :class:`bytearray`\s are accepted too, and in some cases
189 (such as :class:`readinto`) needed. Text I/O classes work with :class:`str`
190 data.
191
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000192 Note that calling any method (even inquiries) on a closed stream is
193 undefined. Implementations may raise :exc:`IOError` in this case.
Georg Brandl014197c2008-04-09 18:40:51 +0000194
195 IOBase (and its subclasses) support the iterator protocol, meaning that an
196 :class:`IOBase` object can be iterated over yielding the lines in a stream.
197
198 IOBase also supports the :keyword:`with` statement. In this example, *fp* is
199 closed after the suite of the with statment is complete::
200
201 with open('spam.txt', 'r') as fp:
202 fp.write('Spam and eggs!')
203
204 :class:`IOBase` provides these methods:
205
206 .. method:: close()
207
208 Flush and close this stream. This method has no effect if the file is
209 already closed.
210
211 .. attribute:: closed
212
213 True if the stream is closed.
214
215 .. method:: fileno()
216
217 Return the underlying file descriptor (an integer) of the stream, if it
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000218 exists. An :exc:`IOError` is raised if the IO object does not use a file
Georg Brandl014197c2008-04-09 18:40:51 +0000219 descriptor.
220
221 .. method:: flush()
222
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000223 Flush the write buffers of the stream if applicable. This is not
224 implemented for read-only and non-blocking streams.
Georg Brandl014197c2008-04-09 18:40:51 +0000225
226 .. method:: isatty()
227
228 Tell if a stream is interactive (connected to a terminal/tty device).
229
230 .. method:: readable()
231
232 Tell if a stream can be read from. If False, :meth:`read` will raise
233 :exc:`IOError`.
234
235 .. method:: readline([limit])
236
237 Read and return a line from the stream. If *limit* is specified, at most
238 *limit* bytes will be read.
239
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000240 The line terminator is always ``b'\n'`` for binary files; for text files,
241 the *newlines* argument to :func:`.open()` can be used to select the line
Georg Brandl014197c2008-04-09 18:40:51 +0000242 terminator(s) recognized.
243
244 .. method:: readlines([hint])
245
246 Return a list of lines from the stream. *hint* can be specified to
247 control the number of lines read: no more lines will be read if the total
248 size (in bytes/characters) of all lines so far exceeds *hint*.
249
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000250 .. method:: seek(offset[, whence])
251
252 Change the stream position to byte offset *offset*. *offset* is
253 interpreted relative to the position indicated by *whence*. Values for
254 *whence* are:
255
256 * ``0`` -- start of stream (the default); *pos* should be zero or positive
257 * ``1`` -- current stream position; *pos* may be negative
258 * ``2`` -- end of stream; *pos* is usually negative
259
260 Return the new absolute position.
261
Georg Brandl014197c2008-04-09 18:40:51 +0000262 .. method:: seekable()
263
264 Tell if a stream supports random IO access. If ``False``, :meth:`seek`,
265 :meth:`tell` and :meth:`truncate` will raise :exc:`IOError`.
266
267 .. method:: tell()
268
269 Return an integer indicating the current stream position.
270
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000271 .. method:: truncate([pos])
272
273 Truncate the file to at most *pos* bytes. *pos* defaults to the current
274 file position, as returned by :meth:`tell`.
275
Georg Brandl014197c2008-04-09 18:40:51 +0000276 .. method:: writable()
277
278 Tell if a stream supports writing. If ``False``, :meth:`write` and
279 :meth:`truncate` will raise :exc:`IOError`.
280
281 .. method:: writelines(lines)
282
283 Write a list of lines to the stream. The lines will not be altered; they
284 must contain line separators.
285
286
287.. class:: RawIOBase
288
289 Base class for raw binary I/O. It inherits :class:`IOBase`. There is no
290 public constructor.
291
292 RawIOBase provides or overrides these methods in addition to those from
293 :class:`IOBase`:
294
Benjamin Petersonb47aace2008-04-09 21:38:38 +0000295 .. method:: read([n])
Georg Brandl014197c2008-04-09 18:40:51 +0000296
297 Read and return all bytes from the stream until EOF, or if *n* is
298 specified, up to *n* bytes. An empty bytes object is returned on EOF;
299 ``None`` is returned if the object is set not to block and has no data to
300 read.
301
Benjamin Petersonb47aace2008-04-09 21:38:38 +0000302 .. method:: readall()
Georg Brandl014197c2008-04-09 18:40:51 +0000303
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000304 Read and return all bytes from the stream until EOF, using multiple calls
305 to the stream.
306
307 .. method:: readinto(b)
308
309 Read up to len(b) bytes into bytearray *b* and return the number of bytes
310 read.
311
312 .. method:: write(b)
313
314 Write the given bytes, *b*, to the underlying raw stream and return the
315 number of bytes written (never less than ``len(b)``).
Georg Brandl014197c2008-04-09 18:40:51 +0000316
317
318Raw File I/O
319------------
320
321.. class:: FileIO(name[, mode])
322
323 :class:`FileIO` represents an OS file containing bytes data. It implements
324 the :class:`RawIOBase` interface (and therefore the :class:`IOBase`
325 interface, too).
326
327 The *mode* can be ``'r'``, ``'w'`` or ``'a'`` for reading (default), writing,
328 or appending. The file will be created if it doesn't exist when opened for
329 writing or appending; it will be truncated when opened for writing. Add a
330 ``'+'`` to the mode to allow simultaneous reading and writing.
331
332 :class:`FileIO` provides or overrides these methods in addition to those from
333 :class:`RawIOBase` and :class:`IOBase`:
334
335 .. attribute:: mode
336
337 The mode as given in the constructor.
338
339 .. attribute:: name
340
341 The file name.
342
343 .. method:: read([n])
344
345 Read and return bytes at most *n* bytes. Only one system call is made, so
346 less data than requested may be returned. In non-blocking mode, ``None``
347 is returned when no data is available.
348
349 .. method:: readall()
350
351 Read and return as bytes all the data from the file. As much as
352 immediately available is returned in non-blocking mode. If the EOF has
353 been reached, ``b''`` is returned.
354
355 .. method:: readinto(bytearray)
356
357 This method should not be used on :class:`FileIO` objects.
358
Georg Brandl014197c2008-04-09 18:40:51 +0000359 .. method:: write(b)
360
361 Write the bytes *b* to the file, and return the number actually written.
362 Only one system call is made, so not all of the data may be written.
363
364
365Buffered Streams
366----------------
367
368.. class:: BufferedIOBase
369
370 Base class for streams that support buffering. It inherits :class:`IOBase`.
371 There is no public constructor.
372
373 The main difference with :class:`RawIOBase` is that the :meth:`read` method
374 supports omitting the *size* argument, and does not have a default
375 implementation that defers to :meth:`readinto`.
376
377 In addition, :meth:`read`, :meth:`readinto`, and :meth:`write` may raise
378 :exc:`BlockingIOError` if the underlying raw stream is in non-blocking mode
379 and not ready; unlike their raw counterparts, they will never return
380 ``None``.
381
382 A typical implementation should not inherit from a :class:`RawIOBase`
383 implementation, but wrap one like :class:`BufferedWriter` and
384 :class:`BufferedReader`.
385
386 :class:`BufferedIOBase` provides or overrides these methods in addition to
387 those from :class:`IOBase`:
388
389 .. method:: read([n])
390
391 Read and return up to *n* bytes. If the argument is omitted, ``None``, or
392 negative, data is read and returned until EOF is reached. An empty bytes
393 object is returned if the stream is already at EOF.
394
395 If the argument is positive, and the underlying raw stream is not
396 interactive, multiple raw reads may be issued to satisfy the byte count
397 (unless EOF is reached first). But for interactive raw streams, at most
398 one raw read will be issued, and a short result does not imply that EOF is
399 imminent.
400
401 A :exc:`BlockingIOError` is raised if the underlying raw stream has no
402 data at the moment.
403
404 .. method:: readinto(b)
405
406 Read up to len(b) bytes into bytearray *b* and return the number of bytes
407 read.
408
409 Like :meth:`read`, multiple reads may be issued to the underlying raw
410 stream, unless the latter is 'interactive.'
411
412 A :exc:`BlockingIOError` is raised if the underlying raw stream has no
413 data at the moment.
414
Georg Brandl014197c2008-04-09 18:40:51 +0000415 .. method:: write(b)
416
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000417 Write the given bytes, *b*, to the underlying raw stream and return the
418 number of bytes written (never less than ``len(b)``).
Georg Brandl014197c2008-04-09 18:40:51 +0000419
420 A :exc:`BlockingIOError` is raised if the buffer is full, and the
421 underlying raw stream cannot accept more data at the moment.
422
423
424.. class:: BytesIO([initial_bytes])
425
426 A stream implementation using an in-memory bytes buffer. It inherits
427 :class:`BufferedIOBase`.
428
429 The argument *initial_bytes* is an optional initial bytearray.
430
431 :class:`BytesIO` provides or overrides these methods in addition to those
432 from :class:`BufferedIOBase` and :class:`IOBase`:
433
434 .. method:: getvalue()
435
436 Return the bytes value of the buffer.
437
438 .. method:: read1()
439
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000440 In :class:`BytesIO`, this is the same as :meth:`read()`.
Georg Brandl014197c2008-04-09 18:40:51 +0000441
442 .. method:: truncate([pos])
443
444 Truncate the file to at most *pos* bytes. *pos* defaults to the current
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000445 stream position, as returned by :meth:`tell()`.
Georg Brandl014197c2008-04-09 18:40:51 +0000446
447
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000448.. class:: BufferedReader(raw[, buffer_size])
Georg Brandl014197c2008-04-09 18:40:51 +0000449
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000450 A buffer for a readable, sequential :class:`BaseRawIO` object. It inherits
Georg Brandl014197c2008-04-09 18:40:51 +0000451 :class:`BufferedIOBase`.
452
453 The constructor creates a :class:`BufferedReader` for the given readable
454 *raw* stream and *buffer_size*. If *buffer_size* is omitted,
455 :data:`DEFAULT_BUFFER_SIZE` is used.
456
457 :class:`BufferedReader` provides or overrides these methods in addition to
458 those from :class:`BufferedIOBase` and :class:`IOBase`:
459
460 .. method:: peek([n])
461
462 Return bytes from a buffer without advancing the position. The argument
463 indicates a desired minimal number of bytes; only one read on the raw
464 stream is done to satisfy it. More than the buffer's size is never
465 returned.
466
467 .. method:: read([n])
468
469 Read and return *n* bytes, or if *n* is not given or negative, until EOF
470 or if the read call would block in non-blocking mode.
471
472 .. method:: read1(n)
473
474 Read and return up to *n* bytes with only one call on the raw stream. If
475 at least one byte is buffered, only buffered bytes are returned.
476 Otherwise, one raw stream read call is made.
477
478
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000479.. class:: BufferedWriter(raw[, buffer_size[, max_buffer_size]])
Georg Brandl014197c2008-04-09 18:40:51 +0000480
481 A buffer for a writeable sequential RawIO object. It inherits
482 :class:`BufferedIOBase`.
483
484 The constructor creates a :class:`BufferedWriter` for the given writeable
485 *raw* stream. If the *buffer_size* is not given, it defaults to
486 :data:`DEAFULT_BUFFER_SIZE`. If *max_buffer_size* is omitted, it defaults to
487 twice the buffer size.
488
489 :class:`BufferedWriter` provides or overrides these methods in addition to
490 those from :class:`BufferedIOBase` and :class:`IOBase`:
491
492 .. method:: flush()
493
494 Force bytes held in the buffer into the raw stream. A
495 :exc:`BlockingIOError` is be raised if the raw stream blocks.
496
497 .. method:: write(b)
498
499 Write bytes *b* onto the raw stream and return the number written. A
500 :exc:`BlockingIOError` is raised when the raw stream blocks.
501
502
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000503.. class:: BufferedRWPair(reader, writer[, buffer_size[, max_buffer_size]])
Georg Brandl014197c2008-04-09 18:40:51 +0000504
505 A buffered writer and reader object together for a raw stream that can be
506 written and read from. It has and supports both :meth:`read`, :meth:`write`,
507 and their variants. This is useful for such applications such as sockets and
508 two-way pipes. It inherits :class:`BufferedIOBase`.
509
510 *reader* and *writer* are :class:`RawIOBase` objects that are readable and
511 writeable respectively. If the *buffer_size* is omitted it defaults to
512 :data:`DEFAULT_BUFFER_SIZE`. The *max_buffer_size* (for the buffered writer)
513 defaults to twice the buffer size.
514
515 :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods.
516
517
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000518.. class:: BufferedRandom(raw[, buffer_size[, max_buffer_size]])
Georg Brandl014197c2008-04-09 18:40:51 +0000519
520 A buffered interface to random access streams. It inherits
521 :class:`BufferedReader` and :class:`BufferedWriter`.
522
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000523 The constructor creates a reader and writer for a seekable raw stream, given
Georg Brandl014197c2008-04-09 18:40:51 +0000524 in the first argument. If the *buffer_size* is omitted it defaults to
525 :data:`DEFAULT_BUFFER_SIZE`. The *max_buffer_size* (for the buffered writer)
526 defaults to twice the buffer size.
527
528 :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or
529 :class:`BufferedWriter` can do.
530
531
532Text I/O
533--------
534
535.. class:: TextIOBase
536
537 Base class for text streams. This class provides a character and line based
538 interface to stream I/O. There is no :meth:`readinto` method because
539 Python's character strings are immutable. It inherits :class:`IOBase`.
540 There is no public constructor.
541
542 :class:`TextIOBase` provides or overrides these methods in addition to those
543 from :class:`IOBase`:
544
545 .. attribute:: encoding
546
547 Return the name of the encoding used to decode the stream's bytes into
548 strings, and to encode strings into bytes.
549
550 .. attribute:: newlines
551
552 Return a string, tuple of strings, or ``None`` indicating the newlines
553 translated so far.
554
555 .. method:: read(n)
556
557 Read and return at most *n* characters from the stream. If *n* is
558 negative or ``None``, read to EOF.
559
560 .. method:: readline()
561
562 Read until newline or EOF and return. If the stream is already at EOF, an
563 empty stream is returned.
564
Georg Brandl014197c2008-04-09 18:40:51 +0000565 .. method:: write(s)
566
567 Write string *s* to the stream and return the number of characters
568 written.
569
570
571.. class:: TextIOWrapper(buffer[, encoding[, errors[, newline[, line_buffering]]]])
572
573 A buffered text stream over a :class:`BufferedIOBase` raw stream, *buffer*.
574 It inherits :class:`TextIOBase`.
575
576 *encoding* gives the name of the encoding that the stream will be decoded or
577 encoded with. It defaults to :func:`locale.getpreferredencoding`.
578
579 *errors* determines the strictness of encoding and decoding (see the errors
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000580 argument of :func:`codecs.register`) and defaults to ``'strict'``.
Georg Brandl014197c2008-04-09 18:40:51 +0000581
582 *newline* can be ``None``, ``''``, ``'\n'``, ``'\r'``, or ``'\r\n'``. It
583 controls the handling of line endings. If it is ``None``, universal newlines
584 is enabled. With this enabled, on input, the lines endings ``'\n'``,
585 ``'\r'``, or ``'\r\n'`` are translated to ``'\n'`` before being returned to
586 the caller. Conversely, on output, ``'\n'`` is translated to the system
587 default line seperator, :data:`os.linesep`. If *newline* is any other of its
588 legal values, that newline becomes the newline when the file is read and it
589 is returned untranslated. On output, ``'\n'`` is converted to the *newline*.
590
591 If *line_buffering* is ``True``, :meth:`flush` is implied when a call to
592 write contains a newline character.
593
594 :class:`TextIOWrapper` provides these methods in addition to those of
595 :class:`TextIOBase` and its parents:
596
597 .. attribute:: errors
598
599 The encoding and decoding error setting.
600
601 .. attribute:: line_buffering
602
603 Whether line buffering is enabled.
604
605
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000606.. class:: StringIO([initial_value[, encoding[, errors[, newline]]]])
Georg Brandl014197c2008-04-09 18:40:51 +0000607
608 An in-memory stream for text. It in inherits :class:`TextIOWrapper`.
609
610 Create a new StringIO stream with an inital value, encoding, error handling,
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000611 and newline setting. See :class:`TextIOWrapper`\'s constructor for more
Georg Brandl014197c2008-04-09 18:40:51 +0000612 information.
613
614 :class:`StringIO` provides these methods in addition to those from
615 :class:`TextIOWrapper` and its parents:
616
617 .. method:: getvalue()
618
619 Return a str representation of the contents of the internal buffer.
620
621
622.. class:: IncrementalNewlineDecoder
623
Benjamin Peterson2c5f8282008-04-13 00:27:46 +0000624 A helper codec that decodes newlines for universal newlines mode. It
625 inherits :class:`codecs.IncrementalDecoder`.
Georg Brandl014197c2008-04-09 18:40:51 +0000626