• No results found

Reading and Writing Files

In document and the Python development team (Page 60-66)

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}

>>> print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '

... 'Dcab: {0[Dcab]:d}'.format(table)) Jack: 4098; Sjoerd: 4127; Dcab: 8637678

This could also be done by passing the table as keyword arguments with the ‘**’ notation.

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}

>>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))

Jack: 4098; Sjoerd: 4127; Dcab: 8637678

This is particularly useful in combination with the built-in function vars(), which returns a dictionary containing all local variables.

For a complete overview of string formatting withstr.format(), see formatstrings.

7.1.1 Old string formatting

The%operator can also be used for string formatting. It interprets the left argument much like a sprintf()-style format string to be applied to the right argument, and returns the string resulting from this formatting operation. For example:

>>> import math

>>> print('The value of PI is approximately %5.3f.' % math.pi)

The value of PI is approximately 3.142.

More information can be found in the old-string-formatting section.

Python Tutorial, Release 3.6.4

>>> with open('workfile') as f:

... read_data = f.read()

>>> f.closed

True

If you’re not using the with keyword, then you should call f.close() to close the file and immediately free up any system resources used by it. If you don’t explicitly close a file, Python’s garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while. Another risk is that different Python implementations will do this clean-up at different times.

After a file object is closed, either by a with statement or by calling f.close(), attempts to use the file object will automatically fail.

>>> f.close()

>>> f.read()

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ValueError: I/O operation on closed file

7.2.1 Methods of File Objects

The rest of the examples in this section will assume that a file object calledfhas already been created.

To read a file’s contents, callf.read(size), which reads some quantity of data and returns it as a string (in text mode) or bytes object (in binary mode). size is an optional numeric argument. Whensizeis omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at mostsizebytes are read and returned. If the end of the file has been reached,f.read()will return an empty string ('').

>>> f.read()

'This is the entire file.\n'

>>> f.read()

''

f.readline()reads a single line from the file; a newline character (\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline. This makes the return value unambiguous; if f.readline()returns an empty string, the end of the file has been reached, while a blank line is represented by'\n', a string containing only a single newline.

>>> f.readline()

'This is the first line of the file.\n'

>>> f.readline()

'Second line of the file\n'

>>> f.readline()

''

For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code:

>>> for line in f:

... print(line, end='') ...

This is the first line of the file.

Second line of the file

If you want to read all the lines of a file in a list you can also uselist(f)orf.readlines().

7.2. Reading and Writing Files 55

f.write(string)writes the contents ofstringto the file, returning the number of characters written.

>>> f.write('This is a test\n')

15

Other types of objects need to be converted – either to a string (in text mode) or a bytes object (in binary mode) – before writing them:

>>> value = ('the answer', 42)

>>> s = str(value) # convert the tuple to string

>>> f.write(s)

18

f.tell()returns an integer giving the file object’s current position in the file represented as number of bytes from the beginning of the file when in binary mode and an opaque number when in text mode.

To change the file object’s position, use f.seek(offset, from_what). The position is computed from addingoffsetto a reference point; the reference point is selected by thefrom_whatargument. Afrom_what value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point. from_what can be omitted and defaults to 0, using the beginning of the file as the reference point.

>>> f = open('workfile', 'rb+')

>>> f.write(b'0123456789abcdef')

16

>>> f.seek(5) # Go to the 6th byte in the file

5

>>> f.read(1)

b'5'

>>> f.seek(-3, 2) # Go to the 3rd byte before the end

13

>>> f.read(1)

b'd'

In text files (those opened without abin the mode string), only seeks relative to the beginning of the file are allowed (the exception being seeking to the very file end withseek(0, 2)) and the only validoffsetvalues are those returned from thef.tell(), or zero. Any otheroffset value produces undefined behaviour.

File objects have some additional methods, such as isatty() and truncate() which are less frequently used; consult the Library Reference for a complete guide to file objects.

7.2.2 Saving structured data with json

Strings can easily be written to and read from a file. Numbers take a bit more effort, since the read() method only returns strings, which will have to be passed to a function likeint(), which takes a string like '123'and returns its numeric value 123. When you want to save more complex data types like nested lists and dictionaries, parsing and serializing by hand becomes complicated.

Rather than having users constantly writing and debugging code to save complicated data types to files, Python allows you to use the popular data interchange format called JSON (JavaScript Object Notation).

The standard module calledjson can take Python data hierarchies, and convert them to string represen-tations; this process is called serializing. Reconstructing the data from the string representation is called deserializing. Between serializing and deserializing, the string representing the object may have been stored in a file or data, or sent over a network connection to some distant machine.

Note: The JSON format is commonly used by modern applications to allow for data exchange. Many

Python Tutorial, Release 3.6.4

programmers are already familiar with it, which makes it a good choice for interoperability.

If you have an objectx, you can view its JSON string representation with a simple line of code:

>>> import json

>>> json.dumps([1, 'simple', 'list'])

'[1, "simple", "list"]'

Another variant of thedumps()function, calleddump(), simply serializes the object to atext file. So if fis atext file object opened for writing, we can do this:

json.dump(x, f)

To decode the object again, iffis a text fileobject which has been opened for reading:

x = json.load(f)

This simple serialization technique can handle lists and dictionaries, but serializing arbitrary class instances in JSON requires a bit of extra effort. The reference for thejson module contains an explanation of this.

See also:

pickle- the pickle module

Contrary toJSON,pickleis a protocol which allows the serialization of arbitrarily complex Python objects.

As such, it is specific to Python and cannot be used to communicate with applications written in other languages. It is also insecure by default: deserializing pickle data coming from an untrusted source can execute arbitrary code, if the data was crafted by a skilled attacker.

7.2. Reading and Writing Files 57

CHAPTER

EIGHT

ERRORS AND EXCEPTIONS

Until now error messages haven’t been more than mentioned, but if you have tried out the examples you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errorsandexceptions.

8.1 Syntax Errors

Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while you are still learning Python:

>>> while True print('Hello world')

File "<stdin>", line 1

while True print('Hello world')

^ SyntaxError: invalid syntax

The parser repeats the offending line and displays a little ‘arrow’ pointing at the earliest point in the line where the error was detected. The error is caused by (or at least detected at) the tokenprecedingthe arrow:

in the example, the error is detected at the functionprint(), since a colon (':') is missing before it. File name and line number are printed so you know where to look in case the input came from a script.

8.2 Exceptions

Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are calledexceptions and are not unconditionally fatal: you will soon learn how to handle them in Python programs. Most exceptions are not handled by programs, however, and result in error messages as shown here:

>>> 10 * (1/0)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ZeroDivisionError: division by zero

>>> 4 + spam*3

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'spam' is not defined

>>> '2' + 2

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

TypeError: Can't convert 'int' object to str implicitly

59

The last line of the error message indicates what happened. Exceptions come in different types, and the type is printed as part of the message: the types in the example areZeroDivisionError, NameErrorand TypeError. The string printed as the exception type is the name of the built-in exception that occurred.

This is true for all built-in exceptions, but need not be true for user-defined exceptions (although it is a useful convention). Standard exception names are built-in identifiers (not reserved keywords).

The rest of the line provides detail based on the type of exception and what caused it.

The preceding part of the error message shows the context where the exception happened, in the form of a stack traceback. In general it contains a stack traceback listing source lines; however, it will not display lines read from standard input.

bltin-exceptions lists the built-in exceptions and their meanings.

In document and the Python development team (Page 60-66)