16.13: Chapter 14
( \newcommand{\kernel}{\mathrm{null}\,}\)
14.1 Reading from files
1.
c. The file is opened and is associated with the fileobj object. Reading from the file will be possible using fileobj.
2.
a. The file must exist in the same folder as the Python file in order for the file to be opened.
3.
b. Python interprets this command as the programmer trying to print the object details. The object details are printed. Ex:
<_io.TextIOWrapper name='newfile.txt' mode='r' encoding='cp1252'>
.
4.
c. The
read()
function is called on fileobj
. The file's contents will be read into file_str
.
5.
b. The
readlines()
function is called on fileobj
. file_lines
is the string list ['Hello world!\n', 'How are you?\n']
.
6.
a. The
readline()
function reads the next line of a file. Since readline()
is called right after opening the file the first line is read. The resulting string "Hello world!"
is printed. Note that readline()
includes the '\n'
character so the print will include an additional newline.
14.2 Writing to files
1.
a. The default mode for the
open()
function is read.
2.
c.
'a'
indicates append mode allowing additions to the file.
3.
b.
'w'
indicates write mode, and newlog.txt will be created or overwritten (if newlog.txt already exists) and will allow writing.
4.
c. resources.txt is likely empty, but the state of resources.txt is uncertain. To ensure the contents are saved correctly, the
close()
function must be used.
5.
a. Write mode will overwrite the existing file. The
close()
statement finalizes changes to the file.
6.
b. Append mode adds to the existing file. resources.txt is opened such that additions can be made to the file.
14.3 Files in different locations and working with CSV files
1.
b. The path followed by the filename enables the file to be opened correctly and in read mode by default.
2.
c. The use of forward slashes / replacing the Windows standard backslashes \ enables the path to be read correctly. The path followed by the filename enables the file to be opened and when reading a file read mode is preferable.
3.
c. The file is created or overwritten and changes can be written into the file.
4.
a. Since the backslashes in the Windows path appear in the string argument, which usually tells Python that this is part of an escape sequence, the backslashes must be ignored using an additional backslash \ character.
5.
a. The newline
\n
character indicates where line breaks are in a file and is used by the readlines()
function to tell lines apart.
6.
b. The cell at the 2nd row and 3rd column contains
'268'
.
7.
c. The first line of the books.csv is read into
csv_read
. The first line is 'Title, Author, Pages'
, which is the same as the first row.
14.4 Handling exceptions
1.
c. The string
"3-4"
is not an integer, so the int()
function raises a ValueError
like before.
2.
a. The filename should end with
.txt
, not .text
.
3.
b. If the line has no spaces, then
len(parts)
will be only 1,
and parts[1]
will be out of range.
4.
b. The
int()
function raises a ValueError
when trying to convert "one"
to an integer, so the program jumps to the except clause.
5.
c. The program crashes and displays this error because the except clause does not handle
ValueError
.
6.
b. If an except clause does not specify a specific error type, then any error type is handled.
14.5 Raising exceptions
1.
a.
"M"
is a valid size, so the Pizza
object is constructed.
2.
c.
PizzaError
is not a built-in exception type.
3.
a. An exception can be raised without providing an error message. In this case, the error message is empty, but the exception name is still printed.