blob: ec0d51ed3335ebeb3df4e6d3a682d0d35327ad46 [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
30Another :class:`IOBase` subclass, :class:`TextIOBase` deals with the encoding
31and 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
35
36Module Interface
37----------------
38
39.. data:: DEFAULT_BUFFER_SIZE
40
41 An int containing the default buffer size used by the module's buffered I/O
42 classes. :func:`open` uses the file's blksize (as obtained by os.stat) if
43 possible.
44
45.. function:: open(file[, mode[, buffering[, encoding[, errors[, newline[, closefd=True]]]]]])
46
47 Open *file* and return a stream.
48
49 *file* is a string giving the name of the file, or an integer file descriptor
50 of the file to be wrapped.
51
52 The optional *mode* string determines how the file is opened and consists of
53 a combination of the following characters:
54
55 ========= ===============================================================
56 Character Meaning
57 --------- ---------------------------------------------------------------
58 ``'r'`` open for reading (default)
59 ``'w'`` open for writing, truncating the file first
60 ``'a'`` open for writing, appending to the end of the file if it exists
61 ``'b'`` binary mode
62 ``'t'`` text mode (default)
63 ``'+'`` open a disk file for updating (reading and writing)
64 ``'U'`` universal newline mode (for backwards compatibility; unneeded
65 for new code)
66 ========= ===============================================================
67
68 The default mode is ``'rt'`` (open for reading text). For binary random
69 access, the mode ``'w+b'`` opens and truncates the file to 0 bytes, while
70 ``'r+b'`` opens the file without truncation.
71
72 *buffering* is an optional argument controling the buffering of the returned
73 stream. A value of ``0`` means no buffering, ``1`` means line buffered, and
74 a greater value means full buffering with the given buffer size. Buffering
75 cannot be disabled in text mode.
76
77 *encoding* is the name of the encoding used to decode or encode the file.
78 This may only be used in text mode. Any encoding available in the
79 :mod:`codecs` module registry can be used.
80
81 *errors* specifies how the encoding should treat errors. "strict", the
82 default raises a :exc:`ValueError` on problems. See the *errors* argument
83 of :func:`codecs.open` for more information. XXX
84
85 *newline* controls how universal newlines works (it only applies to text
86 mode). It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It
87 works as follows:
88
89 * On input, if *newline* is ``None``, universal newlines mode is enabled.
90 Lines in the input can end in ``'\n'``, ``'\r'``, or ``'\r\n'``, and these
91 are translated into ``'\n'`` before being returned to the caller. If it is
92 ``''``, universal newline mode is enabled, but line endings are returned to
93 the caller untranslated. If it has any of the other legal values, input
94 lines are only terminated by the given string, and the line ending is
95 returned to the caller untranslated.
96
97 * On output, if *newline* is ``None``, any ``'\n'`` characters written are
98 translated to the system default line separator, :data:`os.linesep`. If
99 *newline* is ``''``, no translation takes place. If *newline* is any of
100 the other legal values, any ``'\n'`` characters written are translated to
101 the given string.
102
103 If *closefd* is :keyword:`False`, the underlying file descriptor will be kept
104 open when the file is closed. This does not work when a file name is given.
105
106 The :func:`open` function returns a file object whose type depends on the
107 mode, and through which the standard file operations such as reading and
108 writing are performed. When :func:`open` is used to open a file in a text
109 mode (``'w'``, ``'r'``, ``'wt'``, ``'rt'``, etc.), it returns a
110 :class:`TextIOWrapper`. When used to open a file in a binary mode, the
111 returned class varies: in read binary mode, it returns a
112 :class:`BufferedReader`; in write binary and append binary modes, it returns
113 a :class:`BufferedWriter`, and in read/write mode, it returns a
114 :class:`BufferedRandom`.
115
116 It is also possible to use a string or bytearray as a file for both reading
117 and writing. For strings :class:`io.StringIO` can be used like a file opened
118 in a text mode, and for bytes a :class:`io.BytesIO` can be used like a file
119 opened in a binary mode.
120
121
122.. exception:: BlockingIOError
123
124 Error raised when blocking would occur on a non-blocking stream. It inherits
125 :exc:`IOError`.
126
127 In addition to those of :exc:`IOError`, :exc:`BlockingIOError` has one
128 attribute:
129
130 .. attribute:: characters_written
131
132 An integer containing the number of characters written to the stream
133 before it blocked.
134
135
136.. exception:: UnsupportedOperation
137
138 An exception inheriting :exc:`IOError` and :exc:`ValueError` that is raised
139 when an unsupported operation is called on a stream.
140
141
142I/O Base Classes
143----------------
144
145.. class:: IOBase
146
147 The abstract base class for all I/O classes, acting on streams of bytes.
148 There is no public constructor.
149
150 This class provides dummy implementations for many methods that derived
151 classes can override selectively; the default implementations represent a
152 file that cannot be read, written or seeked.
153
154 Even though :class:`IOBase` does not declare :meth:`read`, :meth:`readinto`,
155 :meth:`seek`, or :meth:`write` because their signatures will vary,
156 implementations and clients should consider those methods part of the
157 interface. Also, implementations may raise a :exc:`IOError` when operations
158 they do not support are called.
159
160 The basic type used for binary data read from or written to a file is
161 :class:`bytes`. :class:`bytearray`\s are accepted too, and in some cases
162 (such as :class:`readinto`) needed. Text I/O classes work with :class:`str`
163 data.
164
165 Note that calling any method (even inquiries) on a closed file is undefined.
166 Implementations may raise :exc:`IOError` in this case.
167
168 IOBase (and its subclasses) support the iterator protocol, meaning that an
169 :class:`IOBase` object can be iterated over yielding the lines in a stream.
170
171 IOBase also supports the :keyword:`with` statement. In this example, *fp* is
172 closed after the suite of the with statment is complete::
173
174 with open('spam.txt', 'r') as fp:
175 fp.write('Spam and eggs!')
176
177 :class:`IOBase` provides these methods:
178
179 .. method:: close()
180
181 Flush and close this stream. This method has no effect if the file is
182 already closed.
183
184 .. attribute:: closed
185
186 True if the stream is closed.
187
188 .. method:: fileno()
189
190 Return the underlying file descriptor (an integer) of the stream, if it
191 exists. Raises :exc:`IOError` if the IO object does not use a file
192 descriptor.
193
194 .. method:: flush()
195
196 Flush the write buffers of the stream if applicable. This is a no-op for
197 read-only and non-blocking streams.
198
199 .. method:: isatty()
200
201 Tell if a stream is interactive (connected to a terminal/tty device).
202
203 .. method:: readable()
204
205 Tell if a stream can be read from. If False, :meth:`read` will raise
206 :exc:`IOError`.
207
208 .. method:: readline([limit])
209
210 Read and return a line from the stream. If *limit* is specified, at most
211 *limit* bytes will be read.
212
213 The line terminator is always ``b'\n'`` for binary files; for text files
214 the *newlines* argument to :func:`.open` can be used to select the line
215 terminator(s) recognized.
216
217 .. method:: readlines([hint])
218
219 Return a list of lines from the stream. *hint* can be specified to
220 control the number of lines read: no more lines will be read if the total
221 size (in bytes/characters) of all lines so far exceeds *hint*.
222
223 .. method:: seekable()
224
225 Tell if a stream supports random IO access. If ``False``, :meth:`seek`,
226 :meth:`tell` and :meth:`truncate` will raise :exc:`IOError`.
227
228 .. method:: tell()
229
230 Return an integer indicating the current stream position.
231
232 .. method:: writable()
233
234 Tell if a stream supports writing. If ``False``, :meth:`write` and
235 :meth:`truncate` will raise :exc:`IOError`.
236
237 .. method:: writelines(lines)
238
239 Write a list of lines to the stream. The lines will not be altered; they
240 must contain line separators.
241
242
243.. class:: RawIOBase
244
245 Base class for raw binary I/O. It inherits :class:`IOBase`. There is no
246 public constructor.
247
248 RawIOBase provides or overrides these methods in addition to those from
249 :class:`IOBase`:
250
251 .. method:: RawIOBase.read([n])
252
253 Read and return all bytes from the stream until EOF, or if *n* is
254 specified, up to *n* bytes. An empty bytes object is returned on EOF;
255 ``None`` is returned if the object is set not to block and has no data to
256 read.
257
258 .. method:: RawIOBase.readall()
259
260 Read and return all bytes from the stream until EOF.
261
262
263Raw File I/O
264------------
265
266.. class:: FileIO(name[, mode])
267
268 :class:`FileIO` represents an OS file containing bytes data. It implements
269 the :class:`RawIOBase` interface (and therefore the :class:`IOBase`
270 interface, too).
271
272 The *mode* can be ``'r'``, ``'w'`` or ``'a'`` for reading (default), writing,
273 or appending. The file will be created if it doesn't exist when opened for
274 writing or appending; it will be truncated when opened for writing. Add a
275 ``'+'`` to the mode to allow simultaneous reading and writing.
276
277 :class:`FileIO` provides or overrides these methods in addition to those from
278 :class:`RawIOBase` and :class:`IOBase`:
279
280 .. attribute:: mode
281
282 The mode as given in the constructor.
283
284 .. attribute:: name
285
286 The file name.
287
288 .. method:: read([n])
289
290 Read and return bytes at most *n* bytes. Only one system call is made, so
291 less data than requested may be returned. In non-blocking mode, ``None``
292 is returned when no data is available.
293
294 .. method:: readall()
295
296 Read and return as bytes all the data from the file. As much as
297 immediately available is returned in non-blocking mode. If the EOF has
298 been reached, ``b''`` is returned.
299
300 .. method:: readinto(bytearray)
301
302 This method should not be used on :class:`FileIO` objects.
303
304 .. method:: seek(offset, [whence])
305
306 Change the stream position to byte offset *pos*. *pos* is interpreted
307 relative to the position indicated by *whence*. Values for *whence* are:
308
309 * ``0`` -- start of stream (the default); *pos* should be zero or positive
310 * ``1`` -- current stream position; *pos* may be negative
311 * ``2`` -- end of stream; *pos* is usually negative
312
313 Return the new absolute position.
314
315 .. method:: truncate([pos])
316
317 Truncate the file to at most *pos* bytes. *pos* defaults to the current
318 file position, as returned by :meth:`tell`.
319
320 .. method:: write(b)
321
322 Write the bytes *b* to the file, and return the number actually written.
323 Only one system call is made, so not all of the data may be written.
324
325
326Buffered Streams
327----------------
328
329.. class:: BufferedIOBase
330
331 Base class for streams that support buffering. It inherits :class:`IOBase`.
332 There is no public constructor.
333
334 The main difference with :class:`RawIOBase` is that the :meth:`read` method
335 supports omitting the *size* argument, and does not have a default
336 implementation that defers to :meth:`readinto`.
337
338 In addition, :meth:`read`, :meth:`readinto`, and :meth:`write` may raise
339 :exc:`BlockingIOError` if the underlying raw stream is in non-blocking mode
340 and not ready; unlike their raw counterparts, they will never return
341 ``None``.
342
343 A typical implementation should not inherit from a :class:`RawIOBase`
344 implementation, but wrap one like :class:`BufferedWriter` and
345 :class:`BufferedReader`.
346
347 :class:`BufferedIOBase` provides or overrides these methods in addition to
348 those from :class:`IOBase`:
349
350 .. method:: read([n])
351
352 Read and return up to *n* bytes. If the argument is omitted, ``None``, or
353 negative, data is read and returned until EOF is reached. An empty bytes
354 object is returned if the stream is already at EOF.
355
356 If the argument is positive, and the underlying raw stream is not
357 interactive, multiple raw reads may be issued to satisfy the byte count
358 (unless EOF is reached first). But for interactive raw streams, at most
359 one raw read will be issued, and a short result does not imply that EOF is
360 imminent.
361
362 A :exc:`BlockingIOError` is raised if the underlying raw stream has no
363 data at the moment.
364
365 .. method:: readinto(b)
366
367 Read up to len(b) bytes into bytearray *b* and return the number of bytes
368 read.
369
370 Like :meth:`read`, multiple reads may be issued to the underlying raw
371 stream, unless the latter is 'interactive.'
372
373 A :exc:`BlockingIOError` is raised if the underlying raw stream has no
374 data at the moment.
375
376 .. method:: seek(pos[, whence])
377
378 Change the stream position to byte offset *pos*. *pos* is interpreted
379 relative to the position indicated by *whence*. Values for *whence* are:
380
381 * ``0`` -- start of stream (the default); *pos* should be zero or positive
382 * ``1`` -- current stream position; *pos* may be negative
383 * ``2`` -- end of stream; *pos* is usually negative
384
385 Return the new absolute position.
386
387 .. method:: write(b)
388
389 Write the given bytes to the underlying raw stream and return the number
390 of bytes written (never less than ``len(b)``).
391
392 A :exc:`BlockingIOError` is raised if the buffer is full, and the
393 underlying raw stream cannot accept more data at the moment.
394
395
396.. class:: BytesIO([initial_bytes])
397
398 A stream implementation using an in-memory bytes buffer. It inherits
399 :class:`BufferedIOBase`.
400
401 The argument *initial_bytes* is an optional initial bytearray.
402
403 :class:`BytesIO` provides or overrides these methods in addition to those
404 from :class:`BufferedIOBase` and :class:`IOBase`:
405
406 .. method:: getvalue()
407
408 Return the bytes value of the buffer.
409
410 .. method:: read1()
411
412 In :class:`BytesIO`, this is the same as :meth:`read`.
413
414 .. method:: truncate([pos])
415
416 Truncate the file to at most *pos* bytes. *pos* defaults to the current
417 stream position, as returned by :meth:`tell`.
418
419
420.. class:: BufferedReader(raw, [buffer_size])
421
422 A buffer for a readable sequential RawIO object. It inherits
423 :class:`BufferedIOBase`.
424
425 The constructor creates a :class:`BufferedReader` for the given readable
426 *raw* stream and *buffer_size*. If *buffer_size* is omitted,
427 :data:`DEFAULT_BUFFER_SIZE` is used.
428
429 :class:`BufferedReader` provides or overrides these methods in addition to
430 those from :class:`BufferedIOBase` and :class:`IOBase`:
431
432 .. method:: peek([n])
433
434 Return bytes from a buffer without advancing the position. The argument
435 indicates a desired minimal number of bytes; only one read on the raw
436 stream is done to satisfy it. More than the buffer's size is never
437 returned.
438
439 .. method:: read([n])
440
441 Read and return *n* bytes, or if *n* is not given or negative, until EOF
442 or if the read call would block in non-blocking mode.
443
444 .. method:: read1(n)
445
446 Read and return up to *n* bytes with only one call on the raw stream. If
447 at least one byte is buffered, only buffered bytes are returned.
448 Otherwise, one raw stream read call is made.
449
450
451.. class:: BufferedWriter(raw, [buffer_size, [max_buffer_size]])
452
453 A buffer for a writeable sequential RawIO object. It inherits
454 :class:`BufferedIOBase`.
455
456 The constructor creates a :class:`BufferedWriter` for the given writeable
457 *raw* stream. If the *buffer_size* is not given, it defaults to
458 :data:`DEAFULT_BUFFER_SIZE`. If *max_buffer_size* is omitted, it defaults to
459 twice the buffer size.
460
461 :class:`BufferedWriter` provides or overrides these methods in addition to
462 those from :class:`BufferedIOBase` and :class:`IOBase`:
463
464 .. method:: flush()
465
466 Force bytes held in the buffer into the raw stream. A
467 :exc:`BlockingIOError` is be raised if the raw stream blocks.
468
469 .. method:: write(b)
470
471 Write bytes *b* onto the raw stream and return the number written. A
472 :exc:`BlockingIOError` is raised when the raw stream blocks.
473
474
475.. class:: BufferedRWPair(reader, writer, [buffer_size, [max_buffer_size]])
476
477 A buffered writer and reader object together for a raw stream that can be
478 written and read from. It has and supports both :meth:`read`, :meth:`write`,
479 and their variants. This is useful for such applications such as sockets and
480 two-way pipes. It inherits :class:`BufferedIOBase`.
481
482 *reader* and *writer* are :class:`RawIOBase` objects that are readable and
483 writeable respectively. If the *buffer_size* is omitted it defaults to
484 :data:`DEFAULT_BUFFER_SIZE`. The *max_buffer_size* (for the buffered writer)
485 defaults to twice the buffer size.
486
487 :class:`BufferedRWPair` implements all of :class:`BufferedIOBase`\'s methods.
488
489
490.. class:: BufferedRandom(raw, [buffer_size, [max_buffer_size]])
491
492 A buffered interface to random access streams. It inherits
493 :class:`BufferedReader` and :class:`BufferedWriter`.
494
495 The constructor creates a reader and writer for a seekable *raw* stream given
496 in the first argument. If the *buffer_size* is omitted it defaults to
497 :data:`DEFAULT_BUFFER_SIZE`. The *max_buffer_size* (for the buffered writer)
498 defaults to twice the buffer size.
499
500 :class:`BufferedRandom` is capable of anything :class:`BufferedReader` or
501 :class:`BufferedWriter` can do.
502
503
504Text I/O
505--------
506
507.. class:: TextIOBase
508
509 Base class for text streams. This class provides a character and line based
510 interface to stream I/O. There is no :meth:`readinto` method because
511 Python's character strings are immutable. It inherits :class:`IOBase`.
512 There is no public constructor.
513
514 :class:`TextIOBase` provides or overrides these methods in addition to those
515 from :class:`IOBase`:
516
517 .. attribute:: encoding
518
519 Return the name of the encoding used to decode the stream's bytes into
520 strings, and to encode strings into bytes.
521
522 .. attribute:: newlines
523
524 Return a string, tuple of strings, or ``None`` indicating the newlines
525 translated so far.
526
527 .. method:: read(n)
528
529 Read and return at most *n* characters from the stream. If *n* is
530 negative or ``None``, read to EOF.
531
532 .. method:: readline()
533
534 Read until newline or EOF and return. If the stream is already at EOF, an
535 empty stream is returned.
536
537 .. method:: truncate([pos])
538
539 Truncate size to *pos*. If *pos* is not specified, it is assumed to be the
540 current position, as returned by :meth:`tell`.
541
542 .. method:: write(s)
543
544 Write string *s* to the stream and return the number of characters
545 written.
546
547
548.. class:: TextIOWrapper(buffer[, encoding[, errors[, newline[, line_buffering]]]])
549
550 A buffered text stream over a :class:`BufferedIOBase` raw stream, *buffer*.
551 It inherits :class:`TextIOBase`.
552
553 *encoding* gives the name of the encoding that the stream will be decoded or
554 encoded with. It defaults to :func:`locale.getpreferredencoding`.
555
556 *errors* determines the strictness of encoding and decoding (see the errors
557 argument of :func:`codecs.open`) and defaults to "strict".
558
559 *newline* can be ``None``, ``''``, ``'\n'``, ``'\r'``, or ``'\r\n'``. It
560 controls the handling of line endings. If it is ``None``, universal newlines
561 is enabled. With this enabled, on input, the lines endings ``'\n'``,
562 ``'\r'``, or ``'\r\n'`` are translated to ``'\n'`` before being returned to
563 the caller. Conversely, on output, ``'\n'`` is translated to the system
564 default line seperator, :data:`os.linesep`. If *newline* is any other of its
565 legal values, that newline becomes the newline when the file is read and it
566 is returned untranslated. On output, ``'\n'`` is converted to the *newline*.
567
568 If *line_buffering* is ``True``, :meth:`flush` is implied when a call to
569 write contains a newline character.
570
571 :class:`TextIOWrapper` provides these methods in addition to those of
572 :class:`TextIOBase` and its parents:
573
574 .. attribute:: errors
575
576 The encoding and decoding error setting.
577
578 .. attribute:: line_buffering
579
580 Whether line buffering is enabled.
581
582
583.. class:: StringIO([initial_value, [encoding, [errors, [newline]]]])
584
585 An in-memory stream for text. It in inherits :class:`TextIOWrapper`.
586
587 Create a new StringIO stream with an inital value, encoding, error handling,
588 and newline setting. See :class:`TextIOWrapper`'s constructor for more
589 information.
590
591 :class:`StringIO` provides these methods in addition to those from
592 :class:`TextIOWrapper` and its parents:
593
594 .. method:: getvalue()
595
596 Return a str representation of the contents of the internal buffer.
597
598
599.. class:: IncrementalNewlineDecoder
600
601 A helper codec that decodes newlines. It inherits
602 :class:`codecs.IncrementalDecoder`.
603