6 Chapter 9: Files and Exceptions
with open(‘accounts.json‘, ‘r’) as accounts:
print(json.dumps(json.load(accounts), indent=4))
the string contains newline characters and indentation for pretty printing—you
also can use indent with the dump function when writing to a file:
d. All of the above statements are true.
9.6 Focus on Security: pickle Serialization and
Deserialization
9.6 Q1: Which of the following statements a), b) or c) is false?
a. The Python Standard Library’s pickle module can serialize objects into the
data formats of many popular programming languages.
b. Pickle has potential security issues.
c. If you write your own pickling and de-pickling code, pickling can be secure.
d. All of the above statements are true.
9.7 Additional Notes Regarding Files
9.7 Q1: Which of the following statements a), b) or c) is false?
a. The file open modes for writing and appending raise a FileNotFoundError if
the file does not exist.
b. Each text-file mode has a corresponding binary-file mode specified with b, as
in ‘rb’ or ‘wb+’.
c. You use binary-file modes for reading or writing binary files, such as images,
audio, video, compressed ZIP files and many other popular custom file formats.
d. All of the above statements are true.
9.7 Q2: Which of the following statements is false?
a. For a text file, the read method returns a string containing the number of char-
acters specified by the method’s integer argument. For a binary file, the method
returns the specified number of bytes. If no argument is specified, the method
returns the entire contents of the file.
b. The readline method returns one line of text as a string, including the newline
character if there is one.
c. The readline method issues an EndOfFileError when it encounters the end
of the file.