blob: ca908a3d323aae459b0a44382878ccaf5a5ee141 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. _tut-io:
2
3****************
4Input and Output
5****************
6
7There are several ways to present the output of a program; data can be printed
8in a human-readable form, or written to a file for future use. This chapter will
9discuss some of the possibilities.
10
11
12.. _tut-formatting:
13
14Fancier Output Formatting
15=========================
16
17So far we've encountered two ways of writing values: *expression statements* and
18the :keyword:`print` statement. (A third way is using the :meth:`write` method
19of file objects; the standard output file can be referenced as ``sys.stdout``.
20See the Library Reference for more information on this.)
21
Georg Brandl8ec7f652007-08-15 14:28:01 +000022Often you'll want more control over the formatting of your output than simply
23printing space-separated values. There are two ways to format your output; the
24first way is to do all the string handling yourself; using string slicing and
25concatenation operations you can create any layout you can imagine. The
Georg Brandlcdbc6962011-03-06 10:56:18 +010026string types have some methods that perform useful operations for padding
Georg Brandl8ec7f652007-08-15 14:28:01 +000027strings to a given column width; these will be discussed shortly. The second
Benjamin Petersonf9ef9882008-05-26 00:54:22 +000028way is to use the :meth:`str.format` method.
Georg Brandl8ec7f652007-08-15 14:28:01 +000029
Georg Brandlcdbc6962011-03-06 10:56:18 +010030The :mod:`string` module contains a :class:`~string.Template` class which offers
31yet another way to substitute values into strings.
32
Georg Brandl8ec7f652007-08-15 14:28:01 +000033One question remains, of course: how do you convert values to strings? Luckily,
34Python has ways to convert any value to a string: pass it to the :func:`repr`
Georg Brandlb04d4852008-08-08 15:34:34 +000035or :func:`str` functions.
Georg Brandl8ec7f652007-08-15 14:28:01 +000036
37The :func:`str` function is meant to return representations of values which are
38fairly human-readable, while :func:`repr` is meant to generate representations
39which can be read by the interpreter (or will force a :exc:`SyntaxError` if
40there is not equivalent syntax). For objects which don't have a particular
41representation for human consumption, :func:`str` will return the same value as
42:func:`repr`. Many values, such as numbers or structures like lists and
43dictionaries, have the same representation using either function. Strings and
44floating point numbers, in particular, have two distinct representations.
45
46Some examples::
47
48 >>> s = 'Hello, world.'
49 >>> str(s)
50 'Hello, world.'
51 >>> repr(s)
52 "'Hello, world.'"
Mark Dickinson6b87f112009-11-24 14:27:02 +000053 >>> str(1.0/7.0)
54 '0.142857142857'
55 >>> repr(1.0/7.0)
56 '0.14285714285714285'
Georg Brandl8ec7f652007-08-15 14:28:01 +000057 >>> x = 10 * 3.25
58 >>> y = 200 * 200
59 >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
60 >>> print s
61 The value of x is 32.5, and y is 40000...
62 >>> # The repr() of a string adds string quotes and backslashes:
63 ... hello = 'hello, world\n'
64 >>> hellos = repr(hello)
65 >>> print hellos
66 'hello, world\n'
67 >>> # The argument to repr() may be any Python object:
68 ... repr((x, y, ('spam', 'eggs')))
69 "(32.5, 40000, ('spam', 'eggs'))"
Georg Brandl8ec7f652007-08-15 14:28:01 +000070
71Here are two ways to write a table of squares and cubes::
72
73 >>> for x in range(1, 11):
74 ... print repr(x).rjust(2), repr(x*x).rjust(3),
75 ... # Note trailing comma on previous line
76 ... print repr(x*x*x).rjust(4)
77 ...
78 1 1 1
79 2 4 8
80 3 9 27
81 4 16 64
82 5 25 125
83 6 36 216
84 7 49 343
85 8 64 512
86 9 81 729
87 10 100 1000
88
89 >>> for x in range(1,11):
Benjamin Petersonf9ef9882008-05-26 00:54:22 +000090 ... print '{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x)
Georg Brandlc62ef8b2009-01-03 20:55:06 +000091 ...
Georg Brandl8ec7f652007-08-15 14:28:01 +000092 1 1 1
93 2 4 8
94 3 9 27
95 4 16 64
96 5 25 125
97 6 36 216
98 7 49 343
99 8 64 512
100 9 81 729
101 10 100 1000
102
103(Note that in the first example, one space between each column was added by the
104way :keyword:`print` works: it always adds spaces between its arguments.)
105
106This example demonstrates the :meth:`rjust` method of string objects, which
107right-justifies a string in a field of a given width by padding it with spaces
108on the left. There are similar methods :meth:`ljust` and :meth:`center`. These
109methods do not write anything, they just return a new string. If the input
110string is too long, they don't truncate it, but return it unchanged; this will
111mess up your column lay-out but that's usually better than the alternative,
112which would be lying about a value. (If you really want truncation you can
113always add a slice operation, as in ``x.ljust(n)[:n]``.)
114
115There is another method, :meth:`zfill`, which pads a numeric string on the left
116with zeros. It understands about plus and minus signs::
117
118 >>> '12'.zfill(5)
119 '00012'
120 >>> '-3.14'.zfill(7)
121 '-003.14'
122 >>> '3.14159265359'.zfill(5)
123 '3.14159265359'
124
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000125Basic usage of the :meth:`str.format` method looks like this::
126
Georg Brandl254c17c2009-09-01 07:40:54 +0000127 >>> print 'We are the {} who say "{}!"'.format('knights', 'Ni')
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000128 We are the knights who say "Ni!"
129
130The brackets and characters within them (called format fields) are replaced with
Georg Brandl254c17c2009-09-01 07:40:54 +0000131the objects passed into the :meth:`~str.format` method. A number in the
Georg Brandl14bb28a2009-07-29 17:15:20 +0000132brackets refers to the position of the object passed into the
133:meth:`~str.format` method. ::
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000134
135 >>> print '{0} and {1}'.format('spam', 'eggs')
136 spam and eggs
137 >>> print '{1} and {0}'.format('spam', 'eggs')
138 eggs and spam
139
Georg Brandl14bb28a2009-07-29 17:15:20 +0000140If keyword arguments are used in the :meth:`~str.format` method, their values
141are referred to by using the name of the argument. ::
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000142
Georg Brandl4b99e9b2008-07-26 22:13:29 +0000143 >>> print 'This {food} is {adjective}.'.format(
144 ... food='spam', adjective='absolutely horrible')
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000145 This spam is absolutely horrible.
146
147Positional and keyword arguments can be arbitrarily combined::
148
Georg Brandl4b99e9b2008-07-26 22:13:29 +0000149 >>> print 'The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
150 ... other='Georg')
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000151 The story of Bill, Manfred, and Georg.
152
Georg Brandl254c17c2009-09-01 07:40:54 +0000153``'!s'`` (apply :func:`str`) and ``'!r'`` (apply :func:`repr`) can be used to
154convert the value before it is formatted. ::
155
156 >>> import math
157 >>> print 'The value of PI is approximately {}.'.format(math.pi)
158 The value of PI is approximately 3.14159265359.
159 >>> print 'The value of PI is approximately {!r}.'.format(math.pi)
160 The value of PI is approximately 3.141592653589793.
161
Georg Brandla1a4bdb2009-07-18 09:06:31 +0000162An optional ``':'`` and format specifier can follow the field name. This allows
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000163greater control over how the value is formatted. The following example
Raymond Hettinger2bd47952011-02-24 00:00:30 +0000164rounds Pi to three places after the decimal.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000165
166 >>> import math
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000167 >>> print 'The value of PI is approximately {0:.3f}.'.format(math.pi)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000168 The value of PI is approximately 3.142.
169
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000170Passing an integer after the ``':'`` will cause that field to be a minimum
Georg Brandl14bb28a2009-07-29 17:15:20 +0000171number of characters wide. This is useful for making tables pretty. ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000172
173 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
174 >>> for name, phone in table.items():
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000175 ... print '{0:10} ==> {1:10d}'.format(name, phone)
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000176 ...
Georg Brandl8ec7f652007-08-15 14:28:01 +0000177 Jack ==> 4098
178 Dcab ==> 7678
179 Sjoerd ==> 4127
180
Georg Brandl8ec7f652007-08-15 14:28:01 +0000181If you have a really long format string that you don't want to split up, it
182would be nice if you could reference the variables to be formatted by name
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000183instead of by position. This can be done by simply passing the dict and using
184square brackets ``'[]'`` to access the keys ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000185
186 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
Georg Brandl4b99e9b2008-07-26 22:13:29 +0000187 >>> print ('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
188 ... 'Dcab: {0[Dcab]:d}'.format(table))
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000189 Jack: 4098; Sjoerd: 4127; Dcab: 8637678
190
191This could also be done by passing the table as keyword arguments with the '**'
Georg Brandl14bb28a2009-07-29 17:15:20 +0000192notation. ::
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000193
194 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
195 >>> print 'Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000196 Jack: 4098; Sjoerd: 4127; Dcab: 8637678
197
198This is particularly useful in combination with the new built-in :func:`vars`
199function, which returns a dictionary containing all local variables.
200
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000201For a complete overview of string formatting with :meth:`str.format`, see
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000202:ref:`formatstrings`.
203
204
205Old string formatting
206---------------------
207
208The ``%`` operator can also be used for string formatting. It interprets the
209left argument much like a :cfunc:`sprintf`\ -style format string to be applied
210to the right argument, and returns the string resulting from this formatting
211operation. For example::
212
213 >>> import math
214 >>> print 'The value of PI is approximately %5.3f.' % math.pi
215 The value of PI is approximately 3.142.
216
217Since :meth:`str.format` is quite new, a lot of Python code still uses the ``%``
Georg Brandla1a4bdb2009-07-18 09:06:31 +0000218operator. However, because this old style of formatting will eventually be
219removed from the language, :meth:`str.format` should generally be used.
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000220
221More information can be found in the :ref:`string-formatting` section.
222
Georg Brandl8ec7f652007-08-15 14:28:01 +0000223
224.. _tut-files:
225
226Reading and Writing Files
227=========================
228
229.. index::
230 builtin: open
231 object: file
232
233:func:`open` returns a file object, and is most commonly used with two
234arguments: ``open(filename, mode)``.
235
Georg Brandl8ec7f652007-08-15 14:28:01 +0000236::
237
Georg Brandlb19be572007-12-29 10:57:00 +0000238 >>> f = open('/tmp/workfile', 'w')
Georg Brandl8ec7f652007-08-15 14:28:01 +0000239 >>> print f
240 <open file '/tmp/workfile', mode 'w' at 80a0960>
241
242The first argument is a string containing the filename. The second argument is
243another string containing a few characters describing the way in which the file
244will be used. *mode* can be ``'r'`` when the file will only be read, ``'w'``
245for only writing (an existing file with the same name will be erased), and
246``'a'`` opens the file for appending; any data written to the file is
247automatically added to the end. ``'r+'`` opens the file for both reading and
248writing. The *mode* argument is optional; ``'r'`` will be assumed if it's
249omitted.
250
Georg Brandl9af94982008-09-13 17:41:16 +0000251On Windows, ``'b'`` appended to the mode opens the file in binary mode, so there
Michael Foord60931a52009-09-13 16:13:36 +0000252are also modes like ``'rb'``, ``'wb'``, and ``'r+b'``. Python on Windows makes
253a distinction between text and binary files; the end-of-line characters in text
Georg Brandl9af94982008-09-13 17:41:16 +0000254files are automatically altered slightly when data is read or written. This
255behind-the-scenes modification to file data is fine for ASCII text files, but
256it'll corrupt binary data like that in :file:`JPEG` or :file:`EXE` files. Be
257very careful to use binary mode when reading and writing such files. On Unix,
258it doesn't hurt to append a ``'b'`` to the mode, so you can use it
259platform-independently for all binary files.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000260
261
262.. _tut-filemethods:
263
264Methods of File Objects
265-----------------------
266
267The rest of the examples in this section will assume that a file object called
268``f`` has already been created.
269
270To read a file's contents, call ``f.read(size)``, which reads some quantity of
271data and returns it as a string. *size* is an optional numeric argument. When
272*size* is omitted or negative, the entire contents of the file will be read and
273returned; it's your problem if the file is twice as large as your machine's
274memory. Otherwise, at most *size* bytes are read and returned. If the end of
275the file has been reached, ``f.read()`` will return an empty string (``""``).
276::
277
278 >>> f.read()
279 'This is the entire file.\n'
280 >>> f.read()
281 ''
282
283``f.readline()`` reads a single line from the file; a newline character (``\n``)
284is left at the end of the string, and is only omitted on the last line of the
285file if the file doesn't end in a newline. This makes the return value
286unambiguous; if ``f.readline()`` returns an empty string, the end of the file
287has been reached, while a blank line is represented by ``'\n'``, a string
288containing only a single newline. ::
289
290 >>> f.readline()
291 'This is the first line of the file.\n'
292 >>> f.readline()
293 'Second line of the file\n'
294 >>> f.readline()
295 ''
296
297``f.readlines()`` returns a list containing all the lines of data in the file.
298If given an optional parameter *sizehint*, it reads that many bytes from the
299file and enough more to complete a line, and returns the lines from that. This
300is often used to allow efficient reading of a large file by lines, but without
301having to load the entire file in memory. Only complete lines will be returned.
302::
303
304 >>> f.readlines()
305 ['This is the first line of the file.\n', 'Second line of the file\n']
306
Georg Brandl5d242ee2007-09-20 08:44:59 +0000307An alternative approach to reading lines is to loop over the file object. This is
Georg Brandl8ec7f652007-08-15 14:28:01 +0000308memory efficient, fast, and leads to simpler code::
309
310 >>> for line in f:
311 print line,
312
313 This is the first line of the file.
314 Second line of the file
315
316The alternative approach is simpler but does not provide as fine-grained
317control. Since the two approaches manage line buffering differently, they
318should not be mixed.
319
320``f.write(string)`` writes the contents of *string* to the file, returning
321``None``. ::
322
323 >>> f.write('This is a test\n')
324
325To write something other than a string, it needs to be converted to a string
326first::
327
328 >>> value = ('the answer', 42)
329 >>> s = str(value)
330 >>> f.write(s)
331
332``f.tell()`` returns an integer giving the file object's current position in the
333file, measured in bytes from the beginning of the file. To change the file
334object's position, use ``f.seek(offset, from_what)``. The position is computed
335from adding *offset* to a reference point; the reference point is selected by
336the *from_what* argument. A *from_what* value of 0 measures from the beginning
337of the file, 1 uses the current file position, and 2 uses the end of the file as
338the reference point. *from_what* can be omitted and defaults to 0, using the
339beginning of the file as the reference point. ::
340
341 >>> f = open('/tmp/workfile', 'r+')
342 >>> f.write('0123456789abcdef')
343 >>> f.seek(5) # Go to the 6th byte in the file
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000344 >>> f.read(1)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000345 '5'
346 >>> f.seek(-3, 2) # Go to the 3rd byte before the end
347 >>> f.read(1)
348 'd'
349
350When you're done with a file, call ``f.close()`` to close it and free up any
351system resources taken up by the open file. After calling ``f.close()``,
352attempts to use the file object will automatically fail. ::
353
354 >>> f.close()
355 >>> f.read()
356 Traceback (most recent call last):
357 File "<stdin>", line 1, in ?
358 ValueError: I/O operation on closed file
359
Georg Brandla66bb0a2008-07-16 23:35:54 +0000360It is good practice to use the :keyword:`with` keyword when dealing with file
361objects. This has the advantage that the file is properly closed after its
362suite finishes, even if an exception is raised on the way. It is also much
363shorter than writing equivalent :keyword:`try`\ -\ :keyword:`finally` blocks::
364
365 >>> with open('/tmp/workfile', 'r') as f:
366 ... read_data = f.read()
367 >>> f.closed
368 True
369
Georg Brandl14bb28a2009-07-29 17:15:20 +0000370File objects have some additional methods, such as :meth:`~file.isatty` and
371:meth:`~file.truncate` which are less frequently used; consult the Library
372Reference for a complete guide to file objects.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000373
374
375.. _tut-pickle:
376
377The :mod:`pickle` Module
378------------------------
379
380.. index:: module: pickle
381
382Strings can easily be written to and read from a file. Numbers take a bit more
383effort, since the :meth:`read` method only returns strings, which will have to
384be passed to a function like :func:`int`, which takes a string like ``'123'``
385and returns its numeric value 123. However, when you want to save more complex
386data types like lists, dictionaries, or class instances, things get a lot more
387complicated.
388
389Rather than have users be constantly writing and debugging code to save
390complicated data types, Python provides a standard module called :mod:`pickle`.
391This is an amazing module that can take almost any Python object (even some
392forms of Python code!), and convert it to a string representation; this process
393is called :dfn:`pickling`. Reconstructing the object from the string
394representation is called :dfn:`unpickling`. Between pickling and unpickling,
395the string representing the object may have been stored in a file or data, or
396sent over a network connection to some distant machine.
397
398If you have an object ``x``, and a file object ``f`` that's been opened for
399writing, the simplest way to pickle the object takes only one line of code::
400
401 pickle.dump(x, f)
402
403To unpickle the object again, if ``f`` is a file object which has been opened
404for reading::
405
406 x = pickle.load(f)
407
408(There are other variants of this, used when pickling many objects or when you
409don't want to write the pickled data to a file; consult the complete
410documentation for :mod:`pickle` in the Python Library Reference.)
411
412:mod:`pickle` is the standard way to make Python objects which can be stored and
413reused by other programs or by a future invocation of the same program; the
414technical term for this is a :dfn:`persistent` object. Because :mod:`pickle` is
415so widely used, many authors who write Python extensions take care to ensure
416that new data types such as matrices can be properly pickled and unpickled.
417
418