17db96d56Sopenharmony_ci.. _tut-io: 27db96d56Sopenharmony_ci 37db96d56Sopenharmony_ci**************** 47db96d56Sopenharmony_ciInput and Output 57db96d56Sopenharmony_ci**************** 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ciThere are several ways to present the output of a program; data can be printed 87db96d56Sopenharmony_ciin a human-readable form, or written to a file for future use. This chapter will 97db96d56Sopenharmony_cidiscuss some of the possibilities. 107db96d56Sopenharmony_ci 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_ci.. _tut-formatting: 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ciFancier Output Formatting 157db96d56Sopenharmony_ci========================= 167db96d56Sopenharmony_ci 177db96d56Sopenharmony_ciSo far we've encountered two ways of writing values: *expression statements* and 187db96d56Sopenharmony_cithe :func:`print` function. (A third way is using the :meth:`write` method 197db96d56Sopenharmony_ciof file objects; the standard output file can be referenced as ``sys.stdout``. 207db96d56Sopenharmony_ciSee the Library Reference for more information on this.) 217db96d56Sopenharmony_ci 227db96d56Sopenharmony_ciOften you'll want more control over the formatting of your output than simply 237db96d56Sopenharmony_ciprinting space-separated values. There are several ways to format output. 247db96d56Sopenharmony_ci 257db96d56Sopenharmony_ci* To use :ref:`formatted string literals <tut-f-strings>`, begin a string 267db96d56Sopenharmony_ci with ``f`` or ``F`` before the opening quotation mark or triple quotation mark. 277db96d56Sopenharmony_ci Inside this string, you can write a Python expression between ``{`` and ``}`` 287db96d56Sopenharmony_ci characters that can refer to variables or literal values. 297db96d56Sopenharmony_ci 307db96d56Sopenharmony_ci :: 317db96d56Sopenharmony_ci 327db96d56Sopenharmony_ci >>> year = 2016 337db96d56Sopenharmony_ci >>> event = 'Referendum' 347db96d56Sopenharmony_ci >>> f'Results of the {year} {event}' 357db96d56Sopenharmony_ci 'Results of the 2016 Referendum' 367db96d56Sopenharmony_ci 377db96d56Sopenharmony_ci* The :meth:`str.format` method of strings requires more manual 387db96d56Sopenharmony_ci effort. You'll still use ``{`` and ``}`` to mark where a variable 397db96d56Sopenharmony_ci will be substituted and can provide detailed formatting directives, 407db96d56Sopenharmony_ci but you'll also need to provide the information to be formatted. 417db96d56Sopenharmony_ci 427db96d56Sopenharmony_ci :: 437db96d56Sopenharmony_ci 447db96d56Sopenharmony_ci >>> yes_votes = 42_572_654 457db96d56Sopenharmony_ci >>> no_votes = 43_132_495 467db96d56Sopenharmony_ci >>> percentage = yes_votes / (yes_votes + no_votes) 477db96d56Sopenharmony_ci >>> '{:-9} YES votes {:2.2%}'.format(yes_votes, percentage) 487db96d56Sopenharmony_ci ' 42572654 YES votes 49.67%' 497db96d56Sopenharmony_ci 507db96d56Sopenharmony_ci* Finally, you can do all the string handling yourself by using string slicing and 517db96d56Sopenharmony_ci concatenation operations to create any layout you can imagine. The 527db96d56Sopenharmony_ci string type has some methods that perform useful operations for padding 537db96d56Sopenharmony_ci strings to a given column width. 547db96d56Sopenharmony_ci 557db96d56Sopenharmony_ciWhen you don't need fancy output but just want a quick display of some 567db96d56Sopenharmony_civariables for debugging purposes, you can convert any value to a string with 577db96d56Sopenharmony_cithe :func:`repr` or :func:`str` functions. 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ciThe :func:`str` function is meant to return representations of values which are 607db96d56Sopenharmony_cifairly human-readable, while :func:`repr` is meant to generate representations 617db96d56Sopenharmony_ciwhich can be read by the interpreter (or will force a :exc:`SyntaxError` if 627db96d56Sopenharmony_cithere is no equivalent syntax). For objects which don't have a particular 637db96d56Sopenharmony_cirepresentation for human consumption, :func:`str` will return the same value as 647db96d56Sopenharmony_ci:func:`repr`. Many values, such as numbers or structures like lists and 657db96d56Sopenharmony_cidictionaries, have the same representation using either function. Strings, in 667db96d56Sopenharmony_ciparticular, have two distinct representations. 677db96d56Sopenharmony_ci 687db96d56Sopenharmony_ciSome examples:: 697db96d56Sopenharmony_ci 707db96d56Sopenharmony_ci >>> s = 'Hello, world.' 717db96d56Sopenharmony_ci >>> str(s) 727db96d56Sopenharmony_ci 'Hello, world.' 737db96d56Sopenharmony_ci >>> repr(s) 747db96d56Sopenharmony_ci "'Hello, world.'" 757db96d56Sopenharmony_ci >>> str(1/7) 767db96d56Sopenharmony_ci '0.14285714285714285' 777db96d56Sopenharmony_ci >>> x = 10 * 3.25 787db96d56Sopenharmony_ci >>> y = 200 * 200 797db96d56Sopenharmony_ci >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...' 807db96d56Sopenharmony_ci >>> print(s) 817db96d56Sopenharmony_ci The value of x is 32.5, and y is 40000... 827db96d56Sopenharmony_ci >>> # The repr() of a string adds string quotes and backslashes: 837db96d56Sopenharmony_ci ... hello = 'hello, world\n' 847db96d56Sopenharmony_ci >>> hellos = repr(hello) 857db96d56Sopenharmony_ci >>> print(hellos) 867db96d56Sopenharmony_ci 'hello, world\n' 877db96d56Sopenharmony_ci >>> # The argument to repr() may be any Python object: 887db96d56Sopenharmony_ci ... repr((x, y, ('spam', 'eggs'))) 897db96d56Sopenharmony_ci "(32.5, 40000, ('spam', 'eggs'))" 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ciThe :mod:`string` module contains a :class:`~string.Template` class that offers 927db96d56Sopenharmony_ciyet another way to substitute values into strings, using placeholders like 937db96d56Sopenharmony_ci``$x`` and replacing them with values from a dictionary, but offers much less 947db96d56Sopenharmony_cicontrol of the formatting. 957db96d56Sopenharmony_ci 967db96d56Sopenharmony_ci 977db96d56Sopenharmony_ci.. _tut-f-strings: 987db96d56Sopenharmony_ci 997db96d56Sopenharmony_ciFormatted String Literals 1007db96d56Sopenharmony_ci------------------------- 1017db96d56Sopenharmony_ci 1027db96d56Sopenharmony_ci:ref:`Formatted string literals <f-strings>` (also called f-strings for 1037db96d56Sopenharmony_cishort) let you include the value of Python expressions inside a string by 1047db96d56Sopenharmony_ciprefixing the string with ``f`` or ``F`` and writing expressions as 1057db96d56Sopenharmony_ci``{expression}``. 1067db96d56Sopenharmony_ci 1077db96d56Sopenharmony_ciAn optional format specifier can follow the expression. This allows greater 1087db96d56Sopenharmony_cicontrol over how the value is formatted. The following example rounds pi to 1097db96d56Sopenharmony_cithree places after the decimal:: 1107db96d56Sopenharmony_ci 1117db96d56Sopenharmony_ci >>> import math 1127db96d56Sopenharmony_ci >>> print(f'The value of pi is approximately {math.pi:.3f}.') 1137db96d56Sopenharmony_ci The value of pi is approximately 3.142. 1147db96d56Sopenharmony_ci 1157db96d56Sopenharmony_ciPassing an integer after the ``':'`` will cause that field to be a minimum 1167db96d56Sopenharmony_cinumber of characters wide. This is useful for making columns line up. :: 1177db96d56Sopenharmony_ci 1187db96d56Sopenharmony_ci >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678} 1197db96d56Sopenharmony_ci >>> for name, phone in table.items(): 1207db96d56Sopenharmony_ci ... print(f'{name:10} ==> {phone:10d}') 1217db96d56Sopenharmony_ci ... 1227db96d56Sopenharmony_ci Sjoerd ==> 4127 1237db96d56Sopenharmony_ci Jack ==> 4098 1247db96d56Sopenharmony_ci Dcab ==> 7678 1257db96d56Sopenharmony_ci 1267db96d56Sopenharmony_ciOther modifiers can be used to convert the value before it is formatted. 1277db96d56Sopenharmony_ci``'!a'`` applies :func:`ascii`, ``'!s'`` applies :func:`str`, and ``'!r'`` 1287db96d56Sopenharmony_ciapplies :func:`repr`:: 1297db96d56Sopenharmony_ci 1307db96d56Sopenharmony_ci >>> animals = 'eels' 1317db96d56Sopenharmony_ci >>> print(f'My hovercraft is full of {animals}.') 1327db96d56Sopenharmony_ci My hovercraft is full of eels. 1337db96d56Sopenharmony_ci >>> print(f'My hovercraft is full of {animals!r}.') 1347db96d56Sopenharmony_ci My hovercraft is full of 'eels'. 1357db96d56Sopenharmony_ci 1367db96d56Sopenharmony_ciThe ``=`` specifier can be used to expand an expression to the text of the 1377db96d56Sopenharmony_ciexpression, an equal sign, then the representation of the evaluated expression: 1387db96d56Sopenharmony_ci 1397db96d56Sopenharmony_ci >>> bugs = 'roaches' 1407db96d56Sopenharmony_ci >>> count = 13 1417db96d56Sopenharmony_ci >>> area = 'living room' 1427db96d56Sopenharmony_ci >>> print(f'Debugging {bugs=} {count=} {area=}') 1437db96d56Sopenharmony_ci Debugging bugs='roaches' count=13 area='living room' 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ciSee :ref:`self-documenting expressions <bpo-36817-whatsnew>` for more information 1467db96d56Sopenharmony_cion the ``=`` specifier. For a reference on these format specifications, see 1477db96d56Sopenharmony_cithe reference guide for the :ref:`formatspec`. 1487db96d56Sopenharmony_ci 1497db96d56Sopenharmony_ci.. _tut-string-format: 1507db96d56Sopenharmony_ci 1517db96d56Sopenharmony_ciThe String format() Method 1527db96d56Sopenharmony_ci-------------------------- 1537db96d56Sopenharmony_ci 1547db96d56Sopenharmony_ciBasic usage of the :meth:`str.format` method looks like this:: 1557db96d56Sopenharmony_ci 1567db96d56Sopenharmony_ci >>> print('We are the {} who say "{}!"'.format('knights', 'Ni')) 1577db96d56Sopenharmony_ci We are the knights who say "Ni!" 1587db96d56Sopenharmony_ci 1597db96d56Sopenharmony_ciThe brackets and characters within them (called format fields) are replaced with 1607db96d56Sopenharmony_cithe objects passed into the :meth:`str.format` method. A number in the 1617db96d56Sopenharmony_cibrackets can be used to refer to the position of the object passed into the 1627db96d56Sopenharmony_ci:meth:`str.format` method. :: 1637db96d56Sopenharmony_ci 1647db96d56Sopenharmony_ci >>> print('{0} and {1}'.format('spam', 'eggs')) 1657db96d56Sopenharmony_ci spam and eggs 1667db96d56Sopenharmony_ci >>> print('{1} and {0}'.format('spam', 'eggs')) 1677db96d56Sopenharmony_ci eggs and spam 1687db96d56Sopenharmony_ci 1697db96d56Sopenharmony_ciIf keyword arguments are used in the :meth:`str.format` method, their values 1707db96d56Sopenharmony_ciare referred to by using the name of the argument. :: 1717db96d56Sopenharmony_ci 1727db96d56Sopenharmony_ci >>> print('This {food} is {adjective}.'.format( 1737db96d56Sopenharmony_ci ... food='spam', adjective='absolutely horrible')) 1747db96d56Sopenharmony_ci This spam is absolutely horrible. 1757db96d56Sopenharmony_ci 1767db96d56Sopenharmony_ciPositional and keyword arguments can be arbitrarily combined:: 1777db96d56Sopenharmony_ci 1787db96d56Sopenharmony_ci >>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred', 1797db96d56Sopenharmony_ci ... other='Georg')) 1807db96d56Sopenharmony_ci The story of Bill, Manfred, and Georg. 1817db96d56Sopenharmony_ci 1827db96d56Sopenharmony_ciIf you have a really long format string that you don't want to split up, it 1837db96d56Sopenharmony_ciwould be nice if you could reference the variables to be formatted by name 1847db96d56Sopenharmony_ciinstead of by position. This can be done by simply passing the dict and using 1857db96d56Sopenharmony_cisquare brackets ``'[]'`` to access the keys. :: 1867db96d56Sopenharmony_ci 1877db96d56Sopenharmony_ci >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} 1887db96d56Sopenharmony_ci >>> print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; ' 1897db96d56Sopenharmony_ci ... 'Dcab: {0[Dcab]:d}'.format(table)) 1907db96d56Sopenharmony_ci Jack: 4098; Sjoerd: 4127; Dcab: 8637678 1917db96d56Sopenharmony_ci 1927db96d56Sopenharmony_ciThis could also be done by passing the ``table`` dictionary as keyword arguments with the ``**`` 1937db96d56Sopenharmony_cinotation. :: 1947db96d56Sopenharmony_ci 1957db96d56Sopenharmony_ci >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} 1967db96d56Sopenharmony_ci >>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table)) 1977db96d56Sopenharmony_ci Jack: 4098; Sjoerd: 4127; Dcab: 8637678 1987db96d56Sopenharmony_ci 1997db96d56Sopenharmony_ciThis is particularly useful in combination with the built-in function 2007db96d56Sopenharmony_ci:func:`vars`, which returns a dictionary containing all local variables. 2017db96d56Sopenharmony_ci 2027db96d56Sopenharmony_ciAs an example, the following lines produce a tidily aligned 2037db96d56Sopenharmony_ciset of columns giving integers and their squares and cubes:: 2047db96d56Sopenharmony_ci 2057db96d56Sopenharmony_ci >>> for x in range(1, 11): 2067db96d56Sopenharmony_ci ... print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x)) 2077db96d56Sopenharmony_ci ... 2087db96d56Sopenharmony_ci 1 1 1 2097db96d56Sopenharmony_ci 2 4 8 2107db96d56Sopenharmony_ci 3 9 27 2117db96d56Sopenharmony_ci 4 16 64 2127db96d56Sopenharmony_ci 5 25 125 2137db96d56Sopenharmony_ci 6 36 216 2147db96d56Sopenharmony_ci 7 49 343 2157db96d56Sopenharmony_ci 8 64 512 2167db96d56Sopenharmony_ci 9 81 729 2177db96d56Sopenharmony_ci 10 100 1000 2187db96d56Sopenharmony_ci 2197db96d56Sopenharmony_ciFor a complete overview of string formatting with :meth:`str.format`, see 2207db96d56Sopenharmony_ci:ref:`formatstrings`. 2217db96d56Sopenharmony_ci 2227db96d56Sopenharmony_ci 2237db96d56Sopenharmony_ciManual String Formatting 2247db96d56Sopenharmony_ci------------------------ 2257db96d56Sopenharmony_ci 2267db96d56Sopenharmony_ciHere's the same table of squares and cubes, formatted manually:: 2277db96d56Sopenharmony_ci 2287db96d56Sopenharmony_ci >>> for x in range(1, 11): 2297db96d56Sopenharmony_ci ... print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ') 2307db96d56Sopenharmony_ci ... # Note use of 'end' on previous line 2317db96d56Sopenharmony_ci ... print(repr(x*x*x).rjust(4)) 2327db96d56Sopenharmony_ci ... 2337db96d56Sopenharmony_ci 1 1 1 2347db96d56Sopenharmony_ci 2 4 8 2357db96d56Sopenharmony_ci 3 9 27 2367db96d56Sopenharmony_ci 4 16 64 2377db96d56Sopenharmony_ci 5 25 125 2387db96d56Sopenharmony_ci 6 36 216 2397db96d56Sopenharmony_ci 7 49 343 2407db96d56Sopenharmony_ci 8 64 512 2417db96d56Sopenharmony_ci 9 81 729 2427db96d56Sopenharmony_ci 10 100 1000 2437db96d56Sopenharmony_ci 2447db96d56Sopenharmony_ci(Note that the one space between each column was added by the 2457db96d56Sopenharmony_ciway :func:`print` works: it always adds spaces between its arguments.) 2467db96d56Sopenharmony_ci 2477db96d56Sopenharmony_ciThe :meth:`str.rjust` method of string objects right-justifies a string in a 2487db96d56Sopenharmony_cifield of a given width by padding it with spaces on the left. There are 2497db96d56Sopenharmony_cisimilar methods :meth:`str.ljust` and :meth:`str.center`. These methods do 2507db96d56Sopenharmony_cinot write anything, they just return a new string. If the input string is too 2517db96d56Sopenharmony_cilong, they don't truncate it, but return it unchanged; this will mess up your 2527db96d56Sopenharmony_cicolumn lay-out but that's usually better than the alternative, which would be 2537db96d56Sopenharmony_cilying about a value. (If you really want truncation you can always add a 2547db96d56Sopenharmony_cislice operation, as in ``x.ljust(n)[:n]``.) 2557db96d56Sopenharmony_ci 2567db96d56Sopenharmony_ciThere is another method, :meth:`str.zfill`, which pads a numeric string on the 2577db96d56Sopenharmony_cileft with zeros. It understands about plus and minus signs:: 2587db96d56Sopenharmony_ci 2597db96d56Sopenharmony_ci >>> '12'.zfill(5) 2607db96d56Sopenharmony_ci '00012' 2617db96d56Sopenharmony_ci >>> '-3.14'.zfill(7) 2627db96d56Sopenharmony_ci '-003.14' 2637db96d56Sopenharmony_ci >>> '3.14159265359'.zfill(5) 2647db96d56Sopenharmony_ci '3.14159265359' 2657db96d56Sopenharmony_ci 2667db96d56Sopenharmony_ci 2677db96d56Sopenharmony_ciOld string formatting 2687db96d56Sopenharmony_ci--------------------- 2697db96d56Sopenharmony_ci 2707db96d56Sopenharmony_ciThe % operator (modulo) can also be used for string formatting. Given ``'string' 2717db96d56Sopenharmony_ci% values``, instances of ``%`` in ``string`` are replaced with zero or more 2727db96d56Sopenharmony_cielements of ``values``. This operation is commonly known as string 2737db96d56Sopenharmony_ciinterpolation. For example:: 2747db96d56Sopenharmony_ci 2757db96d56Sopenharmony_ci >>> import math 2767db96d56Sopenharmony_ci >>> print('The value of pi is approximately %5.3f.' % math.pi) 2777db96d56Sopenharmony_ci The value of pi is approximately 3.142. 2787db96d56Sopenharmony_ci 2797db96d56Sopenharmony_ciMore information can be found in the :ref:`old-string-formatting` section. 2807db96d56Sopenharmony_ci 2817db96d56Sopenharmony_ci 2827db96d56Sopenharmony_ci.. _tut-files: 2837db96d56Sopenharmony_ci 2847db96d56Sopenharmony_ciReading and Writing Files 2857db96d56Sopenharmony_ci========================= 2867db96d56Sopenharmony_ci 2877db96d56Sopenharmony_ci.. index:: 2887db96d56Sopenharmony_ci pair: built-in function; open 2897db96d56Sopenharmony_ci pair: object; file 2907db96d56Sopenharmony_ci 2917db96d56Sopenharmony_ci:func:`open` returns a :term:`file object`, and is most commonly used with 2927db96d56Sopenharmony_citwo positional arguments and one keyword argument: 2937db96d56Sopenharmony_ci``open(filename, mode, encoding=None)`` 2947db96d56Sopenharmony_ci 2957db96d56Sopenharmony_ci:: 2967db96d56Sopenharmony_ci 2977db96d56Sopenharmony_ci >>> f = open('workfile', 'w', encoding="utf-8") 2987db96d56Sopenharmony_ci 2997db96d56Sopenharmony_ci.. XXX str(f) is <io.TextIOWrapper object at 0x82e8dc4> 3007db96d56Sopenharmony_ci 3017db96d56Sopenharmony_ci >>> print(f) 3027db96d56Sopenharmony_ci <open file 'workfile', mode 'w' at 80a0960> 3037db96d56Sopenharmony_ci 3047db96d56Sopenharmony_ciThe first argument is a string containing the filename. The second argument is 3057db96d56Sopenharmony_cianother string containing a few characters describing the way in which the file 3067db96d56Sopenharmony_ciwill be used. *mode* can be ``'r'`` when the file will only be read, ``'w'`` 3077db96d56Sopenharmony_cifor only writing (an existing file with the same name will be erased), and 3087db96d56Sopenharmony_ci``'a'`` opens the file for appending; any data written to the file is 3097db96d56Sopenharmony_ciautomatically added to the end. ``'r+'`` opens the file for both reading and 3107db96d56Sopenharmony_ciwriting. The *mode* argument is optional; ``'r'`` will be assumed if it's 3117db96d56Sopenharmony_ciomitted. 3127db96d56Sopenharmony_ci 3137db96d56Sopenharmony_ciNormally, files are opened in :dfn:`text mode`, that means, you read and write 3147db96d56Sopenharmony_cistrings from and to the file, which are encoded in a specific *encoding*. 3157db96d56Sopenharmony_ciIf *encoding* is not specified, the default is platform dependent 3167db96d56Sopenharmony_ci(see :func:`open`). 3177db96d56Sopenharmony_ciBecause UTF-8 is the modern de-facto standard, ``encoding="utf-8"`` is 3187db96d56Sopenharmony_cirecommended unless you know that you need to use a different encoding. 3197db96d56Sopenharmony_ciAppending a ``'b'`` to the mode opens the file in :dfn:`binary mode`. 3207db96d56Sopenharmony_ciBinary mode data is read and written as :class:`bytes` objects. 3217db96d56Sopenharmony_ciYou can not specify *encoding* when opening file in binary mode. 3227db96d56Sopenharmony_ci 3237db96d56Sopenharmony_ciIn text mode, the default when reading is to convert platform-specific line 3247db96d56Sopenharmony_ciendings (``\n`` on Unix, ``\r\n`` on Windows) to just ``\n``. When writing in 3257db96d56Sopenharmony_citext mode, the default is to convert occurrences of ``\n`` back to 3267db96d56Sopenharmony_ciplatform-specific line endings. This behind-the-scenes modification 3277db96d56Sopenharmony_cito file data is fine for text files, but will corrupt binary data like that in 3287db96d56Sopenharmony_ci:file:`JPEG` or :file:`EXE` files. Be very careful to use binary mode when 3297db96d56Sopenharmony_cireading and writing such files. 3307db96d56Sopenharmony_ci 3317db96d56Sopenharmony_ciIt is good practice to use the :keyword:`with` keyword when dealing 3327db96d56Sopenharmony_ciwith file objects. The advantage is that the file is properly closed 3337db96d56Sopenharmony_ciafter its suite finishes, even if an exception is raised at some 3347db96d56Sopenharmony_cipoint. Using :keyword:`!with` is also much shorter than writing 3357db96d56Sopenharmony_ciequivalent :keyword:`try`\ -\ :keyword:`finally` blocks:: 3367db96d56Sopenharmony_ci 3377db96d56Sopenharmony_ci >>> with open('workfile', encoding="utf-8") as f: 3387db96d56Sopenharmony_ci ... read_data = f.read() 3397db96d56Sopenharmony_ci 3407db96d56Sopenharmony_ci >>> # We can check that the file has been automatically closed. 3417db96d56Sopenharmony_ci >>> f.closed 3427db96d56Sopenharmony_ci True 3437db96d56Sopenharmony_ci 3447db96d56Sopenharmony_ciIf you're not using the :keyword:`with` keyword, then you should call 3457db96d56Sopenharmony_ci``f.close()`` to close the file and immediately free up any system 3467db96d56Sopenharmony_ciresources used by it. 3477db96d56Sopenharmony_ci 3487db96d56Sopenharmony_ci.. warning:: 3497db96d56Sopenharmony_ci Calling ``f.write()`` without using the :keyword:`!with` keyword or calling 3507db96d56Sopenharmony_ci ``f.close()`` **might** result in the arguments 3517db96d56Sopenharmony_ci of ``f.write()`` not being completely written to the disk, even if the 3527db96d56Sopenharmony_ci program exits successfully. 3537db96d56Sopenharmony_ci 3547db96d56Sopenharmony_ci.. 3557db96d56Sopenharmony_ci See also https://bugs.python.org/issue17852 3567db96d56Sopenharmony_ci 3577db96d56Sopenharmony_ciAfter a file object is closed, either by a :keyword:`with` statement 3587db96d56Sopenharmony_cior by calling ``f.close()``, attempts to use the file object will 3597db96d56Sopenharmony_ciautomatically fail. :: 3607db96d56Sopenharmony_ci 3617db96d56Sopenharmony_ci >>> f.close() 3627db96d56Sopenharmony_ci >>> f.read() 3637db96d56Sopenharmony_ci Traceback (most recent call last): 3647db96d56Sopenharmony_ci File "<stdin>", line 1, in <module> 3657db96d56Sopenharmony_ci ValueError: I/O operation on closed file. 3667db96d56Sopenharmony_ci 3677db96d56Sopenharmony_ci 3687db96d56Sopenharmony_ci.. _tut-filemethods: 3697db96d56Sopenharmony_ci 3707db96d56Sopenharmony_ciMethods of File Objects 3717db96d56Sopenharmony_ci----------------------- 3727db96d56Sopenharmony_ci 3737db96d56Sopenharmony_ciThe rest of the examples in this section will assume that a file object called 3747db96d56Sopenharmony_ci``f`` has already been created. 3757db96d56Sopenharmony_ci 3767db96d56Sopenharmony_ciTo read a file's contents, call ``f.read(size)``, which reads some quantity of 3777db96d56Sopenharmony_cidata and returns it as a string (in text mode) or bytes object (in binary mode). 3787db96d56Sopenharmony_ci*size* is an optional numeric argument. When *size* is omitted or negative, the 3797db96d56Sopenharmony_cientire contents of the file will be read and returned; it's your problem if the 3807db96d56Sopenharmony_cifile is twice as large as your machine's memory. Otherwise, at most *size* 3817db96d56Sopenharmony_cicharacters (in text mode) or *size* bytes (in binary mode) are read and returned. 3827db96d56Sopenharmony_ciIf the end of the file has been reached, ``f.read()`` will return an empty 3837db96d56Sopenharmony_cistring (``''``). :: 3847db96d56Sopenharmony_ci 3857db96d56Sopenharmony_ci >>> f.read() 3867db96d56Sopenharmony_ci 'This is the entire file.\n' 3877db96d56Sopenharmony_ci >>> f.read() 3887db96d56Sopenharmony_ci '' 3897db96d56Sopenharmony_ci 3907db96d56Sopenharmony_ci``f.readline()`` reads a single line from the file; a newline character (``\n``) 3917db96d56Sopenharmony_ciis left at the end of the string, and is only omitted on the last line of the 3927db96d56Sopenharmony_cifile if the file doesn't end in a newline. This makes the return value 3937db96d56Sopenharmony_ciunambiguous; if ``f.readline()`` returns an empty string, the end of the file 3947db96d56Sopenharmony_cihas been reached, while a blank line is represented by ``'\n'``, a string 3957db96d56Sopenharmony_cicontaining only a single newline. :: 3967db96d56Sopenharmony_ci 3977db96d56Sopenharmony_ci >>> f.readline() 3987db96d56Sopenharmony_ci 'This is the first line of the file.\n' 3997db96d56Sopenharmony_ci >>> f.readline() 4007db96d56Sopenharmony_ci 'Second line of the file\n' 4017db96d56Sopenharmony_ci >>> f.readline() 4027db96d56Sopenharmony_ci '' 4037db96d56Sopenharmony_ci 4047db96d56Sopenharmony_ciFor reading lines from a file, you can loop over the file object. This is memory 4057db96d56Sopenharmony_ciefficient, fast, and leads to simple code:: 4067db96d56Sopenharmony_ci 4077db96d56Sopenharmony_ci >>> for line in f: 4087db96d56Sopenharmony_ci ... print(line, end='') 4097db96d56Sopenharmony_ci ... 4107db96d56Sopenharmony_ci This is the first line of the file. 4117db96d56Sopenharmony_ci Second line of the file 4127db96d56Sopenharmony_ci 4137db96d56Sopenharmony_ciIf you want to read all the lines of a file in a list you can also use 4147db96d56Sopenharmony_ci``list(f)`` or ``f.readlines()``. 4157db96d56Sopenharmony_ci 4167db96d56Sopenharmony_ci``f.write(string)`` writes the contents of *string* to the file, returning 4177db96d56Sopenharmony_cithe number of characters written. :: 4187db96d56Sopenharmony_ci 4197db96d56Sopenharmony_ci >>> f.write('This is a test\n') 4207db96d56Sopenharmony_ci 15 4217db96d56Sopenharmony_ci 4227db96d56Sopenharmony_ciOther types of objects need to be converted -- either to a string (in text mode) 4237db96d56Sopenharmony_cior a bytes object (in binary mode) -- before writing them:: 4247db96d56Sopenharmony_ci 4257db96d56Sopenharmony_ci >>> value = ('the answer', 42) 4267db96d56Sopenharmony_ci >>> s = str(value) # convert the tuple to string 4277db96d56Sopenharmony_ci >>> f.write(s) 4287db96d56Sopenharmony_ci 18 4297db96d56Sopenharmony_ci 4307db96d56Sopenharmony_ci``f.tell()`` returns an integer giving the file object's current position in the file 4317db96d56Sopenharmony_cirepresented as number of bytes from the beginning of the file when in binary mode and 4327db96d56Sopenharmony_cian opaque number when in text mode. 4337db96d56Sopenharmony_ci 4347db96d56Sopenharmony_ciTo change the file object's position, use ``f.seek(offset, whence)``. The position is computed 4357db96d56Sopenharmony_cifrom adding *offset* to a reference point; the reference point is selected by 4367db96d56Sopenharmony_cithe *whence* argument. A *whence* value of 0 measures from the beginning 4377db96d56Sopenharmony_ciof the file, 1 uses the current file position, and 2 uses the end of the file as 4387db96d56Sopenharmony_cithe reference point. *whence* can be omitted and defaults to 0, using the 4397db96d56Sopenharmony_cibeginning of the file as the reference point. :: 4407db96d56Sopenharmony_ci 4417db96d56Sopenharmony_ci >>> f = open('workfile', 'rb+') 4427db96d56Sopenharmony_ci >>> f.write(b'0123456789abcdef') 4437db96d56Sopenharmony_ci 16 4447db96d56Sopenharmony_ci >>> f.seek(5) # Go to the 6th byte in the file 4457db96d56Sopenharmony_ci 5 4467db96d56Sopenharmony_ci >>> f.read(1) 4477db96d56Sopenharmony_ci b'5' 4487db96d56Sopenharmony_ci >>> f.seek(-3, 2) # Go to the 3rd byte before the end 4497db96d56Sopenharmony_ci 13 4507db96d56Sopenharmony_ci >>> f.read(1) 4517db96d56Sopenharmony_ci b'd' 4527db96d56Sopenharmony_ci 4537db96d56Sopenharmony_ciIn text files (those opened without a ``b`` in the mode string), only seeks 4547db96d56Sopenharmony_cirelative to the beginning of the file are allowed (the exception being seeking 4557db96d56Sopenharmony_cito the very file end with ``seek(0, 2)``) and the only valid *offset* values are 4567db96d56Sopenharmony_cithose returned from the ``f.tell()``, or zero. Any other *offset* value produces 4577db96d56Sopenharmony_ciundefined behaviour. 4587db96d56Sopenharmony_ci 4597db96d56Sopenharmony_ciFile objects have some additional methods, such as :meth:`~file.isatty` and 4607db96d56Sopenharmony_ci:meth:`~file.truncate` which are less frequently used; consult the Library 4617db96d56Sopenharmony_ciReference for a complete guide to file objects. 4627db96d56Sopenharmony_ci 4637db96d56Sopenharmony_ci 4647db96d56Sopenharmony_ci.. _tut-json: 4657db96d56Sopenharmony_ci 4667db96d56Sopenharmony_ciSaving structured data with :mod:`json` 4677db96d56Sopenharmony_ci--------------------------------------- 4687db96d56Sopenharmony_ci 4697db96d56Sopenharmony_ci.. index:: pair: module; json 4707db96d56Sopenharmony_ci 4717db96d56Sopenharmony_ciStrings can easily be written to and read from a file. Numbers take a bit more 4727db96d56Sopenharmony_cieffort, since the :meth:`read` method only returns strings, which will have to 4737db96d56Sopenharmony_cibe passed to a function like :func:`int`, which takes a string like ``'123'`` 4747db96d56Sopenharmony_ciand returns its numeric value 123. When you want to save more complex data 4757db96d56Sopenharmony_citypes like nested lists and dictionaries, parsing and serializing by hand 4767db96d56Sopenharmony_cibecomes complicated. 4777db96d56Sopenharmony_ci 4787db96d56Sopenharmony_ciRather than having users constantly writing and debugging code to save 4797db96d56Sopenharmony_cicomplicated data types to files, Python allows you to use the popular data 4807db96d56Sopenharmony_ciinterchange format called `JSON (JavaScript Object Notation) 4817db96d56Sopenharmony_ci<https://json.org>`_. The standard module called :mod:`json` can take Python 4827db96d56Sopenharmony_cidata hierarchies, and convert them to string representations; this process is 4837db96d56Sopenharmony_cicalled :dfn:`serializing`. Reconstructing the data from the string representation 4847db96d56Sopenharmony_ciis called :dfn:`deserializing`. Between serializing and deserializing, the 4857db96d56Sopenharmony_cistring representing the object may have been stored in a file or data, or 4867db96d56Sopenharmony_cisent over a network connection to some distant machine. 4877db96d56Sopenharmony_ci 4887db96d56Sopenharmony_ci.. note:: 4897db96d56Sopenharmony_ci The JSON format is commonly used by modern applications to allow for data 4907db96d56Sopenharmony_ci exchange. Many programmers are already familiar with it, which makes 4917db96d56Sopenharmony_ci it a good choice for interoperability. 4927db96d56Sopenharmony_ci 4937db96d56Sopenharmony_ciIf you have an object ``x``, you can view its JSON string representation with a 4947db96d56Sopenharmony_cisimple line of code:: 4957db96d56Sopenharmony_ci 4967db96d56Sopenharmony_ci >>> import json 4977db96d56Sopenharmony_ci >>> x = [1, 'simple', 'list'] 4987db96d56Sopenharmony_ci >>> json.dumps(x) 4997db96d56Sopenharmony_ci '[1, "simple", "list"]' 5007db96d56Sopenharmony_ci 5017db96d56Sopenharmony_ciAnother variant of the :func:`~json.dumps` function, called :func:`~json.dump`, 5027db96d56Sopenharmony_cisimply serializes the object to a :term:`text file`. So if ``f`` is a 5037db96d56Sopenharmony_ci:term:`text file` object opened for writing, we can do this:: 5047db96d56Sopenharmony_ci 5057db96d56Sopenharmony_ci json.dump(x, f) 5067db96d56Sopenharmony_ci 5077db96d56Sopenharmony_ciTo decode the object again, if ``f`` is a :term:`binary file` or 5087db96d56Sopenharmony_ci:term:`text file` object which has been opened for reading:: 5097db96d56Sopenharmony_ci 5107db96d56Sopenharmony_ci x = json.load(f) 5117db96d56Sopenharmony_ci 5127db96d56Sopenharmony_ci.. note:: 5137db96d56Sopenharmony_ci JSON files must be encoded in UTF-8. Use ``encoding="utf-8"`` when opening 5147db96d56Sopenharmony_ci JSON file as a :term:`text file` for both of reading and writing. 5157db96d56Sopenharmony_ci 5167db96d56Sopenharmony_ciThis simple serialization technique can handle lists and dictionaries, but 5177db96d56Sopenharmony_ciserializing arbitrary class instances in JSON requires a bit of extra effort. 5187db96d56Sopenharmony_ciThe reference for the :mod:`json` module contains an explanation of this. 5197db96d56Sopenharmony_ci 5207db96d56Sopenharmony_ci.. seealso:: 5217db96d56Sopenharmony_ci 5227db96d56Sopenharmony_ci :mod:`pickle` - the pickle module 5237db96d56Sopenharmony_ci 5247db96d56Sopenharmony_ci Contrary to :ref:`JSON <tut-json>`, *pickle* is a protocol which allows 5257db96d56Sopenharmony_ci the serialization of arbitrarily complex Python objects. As such, it is 5267db96d56Sopenharmony_ci specific to Python and cannot be used to communicate with applications 5277db96d56Sopenharmony_ci written in other languages. It is also insecure by default: 5287db96d56Sopenharmony_ci deserializing pickle data coming from an untrusted source can execute 5297db96d56Sopenharmony_ci arbitrary code, if the data was crafted by a skilled attacker. 530