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