blob: cfea7bbd57fbd7fc313aaba57923f4723410737f [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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
Guido van Rossum0616b792007-08-31 03:25:11 +000018the :func:`print` function. (A third way is using the :meth:`write` method
Georg Brandl116aa622007-08-15 14:28:22 +000019of file objects; the standard output file can be referenced as ``sys.stdout``.
20See the Library Reference for more information on this.)
21
22.. index:: module: string
23
24Often you'll want more control over the formatting of your output than simply
25printing space-separated values. There are two ways to format your output; the
26first way is to do all the string handling yourself; using string slicing and
27concatenation operations you can create any layout you can imagine. The
28standard module :mod:`string` contains some useful operations for padding
29strings to a given column width; these will be discussed shortly. The second
30way is to use the ``%`` operator with a string as the left argument. The ``%``
31operator interprets the left argument much like a :cfunc:`sprintf`\ -style
32format string to be applied to the right argument, and returns the string
33resulting from this formatting operation.
34
35One question remains, of course: how do you convert values to strings? Luckily,
36Python has ways to convert any value to a string: pass it to the :func:`repr`
37or :func:`str` functions. Reverse quotes (``````) are equivalent to
38:func:`repr`, but they are no longer used in modern Python code and will likely
39not be in future versions of the language.
40
41The :func:`str` function is meant to return representations of values which are
42fairly human-readable, while :func:`repr` is meant to generate representations
43which can be read by the interpreter (or will force a :exc:`SyntaxError` if
44there is not equivalent syntax). For objects which don't have a particular
45representation for human consumption, :func:`str` will return the same value as
46:func:`repr`. Many values, such as numbers or structures like lists and
47dictionaries, have the same representation using either function. Strings and
48floating point numbers, in particular, have two distinct representations.
49
50Some examples::
51
52 >>> s = 'Hello, world.'
53 >>> str(s)
54 'Hello, world.'
55 >>> repr(s)
56 "'Hello, world.'"
57 >>> str(0.1)
58 '0.1'
59 >>> repr(0.1)
60 '0.10000000000000001'
61 >>> x = 10 * 3.25
62 >>> y = 200 * 200
63 >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
Guido van Rossum0616b792007-08-31 03:25:11 +000064 >>> print(s)
Georg Brandl116aa622007-08-15 14:28:22 +000065 The value of x is 32.5, and y is 40000...
66 >>> # The repr() of a string adds string quotes and backslashes:
67 ... hello = 'hello, world\n'
68 >>> hellos = repr(hello)
Guido van Rossum0616b792007-08-31 03:25:11 +000069 >>> print(hellos)
Georg Brandl116aa622007-08-15 14:28:22 +000070 'hello, world\n'
71 >>> # The argument to repr() may be any Python object:
72 ... repr((x, y, ('spam', 'eggs')))
73 "(32.5, 40000, ('spam', 'eggs'))"
74 >>> # reverse quotes are convenient in interactive sessions:
75 ... `x, y, ('spam', 'eggs')`
76 "(32.5, 40000, ('spam', 'eggs'))"
77
78Here are two ways to write a table of squares and cubes::
79
80 >>> for x in range(1, 11):
Georg Brandle4ac7502007-09-03 07:10:24 +000081 ... print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')
Guido van Rossum0616b792007-08-31 03:25:11 +000082 ... # Note use of 'end' on previous line
83 ... print(repr(x*x*x).rjust(4))
Georg Brandl116aa622007-08-15 14:28:22 +000084 ...
85 1 1 1
86 2 4 8
87 3 9 27
88 4 16 64
89 5 25 125
90 6 36 216
91 7 49 343
92 8 64 512
93 9 81 729
94 10 100 1000
95
Georg Brandle4ac7502007-09-03 07:10:24 +000096 >>> for x in range(1, 11):
Guido van Rossum0616b792007-08-31 03:25:11 +000097 ... print('%2d %3d %4d' % (x, x*x, x*x*x))
Georg Brandl116aa622007-08-15 14:28:22 +000098 ...
99 1 1 1
100 2 4 8
101 3 9 27
102 4 16 64
103 5 25 125
104 6 36 216
105 7 49 343
106 8 64 512
107 9 81 729
108 10 100 1000
109
110(Note that in the first example, one space between each column was added by the
Guido van Rossum0616b792007-08-31 03:25:11 +0000111way :func:`print` works: it always adds spaces between its arguments.)
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113This example demonstrates the :meth:`rjust` method of string objects, which
114right-justifies a string in a field of a given width by padding it with spaces
115on the left. There are similar methods :meth:`ljust` and :meth:`center`. These
116methods do not write anything, they just return a new string. If the input
117string is too long, they don't truncate it, but return it unchanged; this will
118mess up your column lay-out but that's usually better than the alternative,
119which would be lying about a value. (If you really want truncation you can
120always add a slice operation, as in ``x.ljust(n)[:n]``.)
121
122There is another method, :meth:`zfill`, which pads a numeric string on the left
123with zeros. It understands about plus and minus signs::
124
125 >>> '12'.zfill(5)
126 '00012'
127 >>> '-3.14'.zfill(7)
128 '-003.14'
129 >>> '3.14159265359'.zfill(5)
130 '3.14159265359'
131
132Using the ``%`` operator looks like this::
133
134 >>> import math
Georg Brandl6911e3c2007-09-04 07:15:32 +0000135 >>> print('The value of PI is approximately %5.3f.' % math.pi)
Georg Brandl116aa622007-08-15 14:28:22 +0000136 The value of PI is approximately 3.142.
137
138If there is more than one format in the string, you need to pass a tuple as
139right operand, as in this example::
140
141 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
142 >>> for name, phone in table.items():
Georg Brandl6911e3c2007-09-04 07:15:32 +0000143 ... print('%-10s ==> %10d' % (name, phone))
Georg Brandl116aa622007-08-15 14:28:22 +0000144 ...
145 Jack ==> 4098
146 Dcab ==> 7678
147 Sjoerd ==> 4127
148
149Most formats work exactly as in C and require that you pass the proper type;
150however, if you don't you get an exception, not a core dump. The ``%s`` format
151is more relaxed: if the corresponding argument is not a string object, it is
152converted to string using the :func:`str` built-in function. Using ``*`` to
153pass the width or precision in as a separate (integer) argument is supported.
154The C formats ``%n`` and ``%p`` are not supported.
155
156If you have a really long format string that you don't want to split up, it
157would be nice if you could reference the variables to be formatted by name
158instead of by position. This can be done by using form ``%(name)format``, as
159shown here::
160
161 >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
Georg Brandl6911e3c2007-09-04 07:15:32 +0000162 >>> print('Jack: %(Jack)d; Sjoerd: %(Sjoerd)d; Dcab: %(Dcab)d' % table)
Georg Brandl116aa622007-08-15 14:28:22 +0000163 Jack: 4098; Sjoerd: 4127; Dcab: 8637678
164
165This is particularly useful in combination with the new built-in :func:`vars`
166function, which returns a dictionary containing all local variables.
167
Guido van Rossum0616b792007-08-31 03:25:11 +0000168The :mod:`string` module contains a class Template which offers yet another way
169to substitute values into strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000170
171.. _tut-files:
172
173Reading and Writing Files
174=========================
175
176.. index::
177 builtin: open
178 object: file
179
180:func:`open` returns a file object, and is most commonly used with two
181arguments: ``open(filename, mode)``.
182
183.. % Opening files
184
185::
186
187 >>> f=open('/tmp/workfile', 'w')
Guido van Rossum0616b792007-08-31 03:25:11 +0000188 >>> print(f)
Georg Brandl116aa622007-08-15 14:28:22 +0000189 <open file '/tmp/workfile', mode 'w' at 80a0960>
190
191The first argument is a string containing the filename. The second argument is
192another string containing a few characters describing the way in which the file
193will be used. *mode* can be ``'r'`` when the file will only be read, ``'w'``
194for only writing (an existing file with the same name will be erased), and
195``'a'`` opens the file for appending; any data written to the file is
196automatically added to the end. ``'r+'`` opens the file for both reading and
197writing. The *mode* argument is optional; ``'r'`` will be assumed if it's
198omitted.
199
Skip Montanaro4e02c502007-09-26 01:10:12 +0000200``'b'`` appended to the mode opens the file in binary mode, so there are
201also modes like ``'rb'``, ``'wb'``, and ``'r+b'``. Python distinguishes
202between text and binary files. Binary files are read and written without
203any data transformation. In text mode, platform-specific newline
204representations are automatically converted to newlines when read and
205newline characters are automatically converted to the proper
206platform-specific representation when written. This makes writing portable
207code which reads or writes text files easier. In addition, when reading
208from or writing to text files, the data are automatically decoded or
209encoding, respectively, using the encoding associated with the file.
210
211This behind-the-scenes modification to file data is fine for text files, but
212will corrupt binary data like that in :file:`JPEG` or :file:`EXE` files. Be
213very careful to use binary mode when reading and writing such files.
Georg Brandl116aa622007-08-15 14:28:22 +0000214
215
216.. _tut-filemethods:
217
218Methods of File Objects
219-----------------------
220
221The rest of the examples in this section will assume that a file object called
222``f`` has already been created.
223
224To read a file's contents, call ``f.read(size)``, which reads some quantity of
225data and returns it as a string. *size* is an optional numeric argument. When
226*size* is omitted or negative, the entire contents of the file will be read and
227returned; it's your problem if the file is twice as large as your machine's
228memory. Otherwise, at most *size* bytes are read and returned. If the end of
229the file has been reached, ``f.read()`` will return an empty string (``""``).
230::
231
232 >>> f.read()
233 'This is the entire file.\n'
234 >>> f.read()
235 ''
236
237``f.readline()`` reads a single line from the file; a newline character (``\n``)
238is left at the end of the string, and is only omitted on the last line of the
239file if the file doesn't end in a newline. This makes the return value
240unambiguous; if ``f.readline()`` returns an empty string, the end of the file
241has been reached, while a blank line is represented by ``'\n'``, a string
242containing only a single newline. ::
243
244 >>> f.readline()
245 'This is the first line of the file.\n'
246 >>> f.readline()
247 'Second line of the file\n'
248 >>> f.readline()
249 ''
250
251``f.readlines()`` returns a list containing all the lines of data in the file.
252If given an optional parameter *sizehint*, it reads that many bytes from the
253file and enough more to complete a line, and returns the lines from that. This
254is often used to allow efficient reading of a large file by lines, but without
255having to load the entire file in memory. Only complete lines will be returned.
256::
257
258 >>> f.readlines()
259 ['This is the first line of the file.\n', 'Second line of the file\n']
260
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000261An alternative approach to reading lines is to loop over the file object. This is
Georg Brandl116aa622007-08-15 14:28:22 +0000262memory efficient, fast, and leads to simpler code::
263
264 >>> for line in f:
Guido van Rossum0616b792007-08-31 03:25:11 +0000265 print(line, end='')
Georg Brandl116aa622007-08-15 14:28:22 +0000266
267 This is the first line of the file.
268 Second line of the file
269
270The alternative approach is simpler but does not provide as fine-grained
271control. Since the two approaches manage line buffering differently, they
272should not be mixed.
273
274``f.write(string)`` writes the contents of *string* to the file, returning
275``None``. ::
276
277 >>> f.write('This is a test\n')
278
279To write something other than a string, it needs to be converted to a string
280first::
281
282 >>> value = ('the answer', 42)
283 >>> s = str(value)
284 >>> f.write(s)
285
286``f.tell()`` returns an integer giving the file object's current position in the
287file, measured in bytes from the beginning of the file. To change the file
288object's position, use ``f.seek(offset, from_what)``. The position is computed
289from adding *offset* to a reference point; the reference point is selected by
290the *from_what* argument. A *from_what* value of 0 measures from the beginning
291of the file, 1 uses the current file position, and 2 uses the end of the file as
292the reference point. *from_what* can be omitted and defaults to 0, using the
293beginning of the file as the reference point. ::
294
295 >>> f = open('/tmp/workfile', 'r+')
296 >>> f.write('0123456789abcdef')
297 >>> f.seek(5) # Go to the 6th byte in the file
298 >>> f.read(1)
299 '5'
300 >>> f.seek(-3, 2) # Go to the 3rd byte before the end
301 >>> f.read(1)
302 'd'
303
304When you're done with a file, call ``f.close()`` to close it and free up any
305system resources taken up by the open file. After calling ``f.close()``,
306attempts to use the file object will automatically fail. ::
307
308 >>> f.close()
309 >>> f.read()
310 Traceback (most recent call last):
311 File "<stdin>", line 1, in ?
312 ValueError: I/O operation on closed file
313
314File objects have some additional methods, such as :meth:`isatty` and
315:meth:`truncate` which are less frequently used; consult the Library Reference
316for a complete guide to file objects.
317
318
319.. _tut-pickle:
320
321The :mod:`pickle` Module
322------------------------
323
324.. index:: module: pickle
325
326Strings can easily be written to and read from a file. Numbers take a bit more
327effort, since the :meth:`read` method only returns strings, which will have to
328be passed to a function like :func:`int`, which takes a string like ``'123'``
329and returns its numeric value 123. However, when you want to save more complex
330data types like lists, dictionaries, or class instances, things get a lot more
331complicated.
332
333Rather than have users be constantly writing and debugging code to save
334complicated data types, Python provides a standard module called :mod:`pickle`.
335This is an amazing module that can take almost any Python object (even some
336forms of Python code!), and convert it to a string representation; this process
337is called :dfn:`pickling`. Reconstructing the object from the string
338representation is called :dfn:`unpickling`. Between pickling and unpickling,
339the string representing the object may have been stored in a file or data, or
340sent over a network connection to some distant machine.
341
342If you have an object ``x``, and a file object ``f`` that's been opened for
343writing, the simplest way to pickle the object takes only one line of code::
344
345 pickle.dump(x, f)
346
347To unpickle the object again, if ``f`` is a file object which has been opened
348for reading::
349
350 x = pickle.load(f)
351
352(There are other variants of this, used when pickling many objects or when you
353don't want to write the pickled data to a file; consult the complete
354documentation for :mod:`pickle` in the Python Library Reference.)
355
356:mod:`pickle` is the standard way to make Python objects which can be stored and
357reused by other programs or by a future invocation of the same program; the
358technical term for this is a :dfn:`persistent` object. Because :mod:`pickle` is
359so widely used, many authors who write Python extensions take care to ensure
360that new data types such as matrices can be properly pickled and unpickled.
361
362