1 |
h07 |
CS8 M19-A |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | section |
||
Optional: name you wish to be called if different from name above. | ||||
Optional: name of "homework buddy" (leaving this blank signifies "I worked alone" |
h07: Perkovic 4.3 (Files) 4.4 (Errors and Exceptions)
ready? | assigned | due | points |
---|---|---|---|
true | Tue 07/09 09:30AM | Tue 07/16 09:30AM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the lowest scores (if you have zeros, those are the lowest scores.)
READING ASSIGNMENT
Please read Perkovic 4.3 (Files) 4.4 (Errors and Exceptions). Then complete these problems and turn in your completed homework in lecture on the due date.
- (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. Put the time your discussion section starts () in the space indicated (the one you are registered for—even if you usually attend a different one.) If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class.
-
One important abstraction provided by most operating systems (Windows, Linux, or MacOS, for example) is something called a “file system”.
-
(10 pts) As was discussed in Chapter 1, one of the purposes of an abstraction is to provide a uniform way of treating something that hides irrelevant detail. Our text points out an important detail of storing information that a file system helps us to ignore. What is that detail?
-
(10 pts) According to our text, every file on a file system can be referred to be an “absolute pathname”, which consists of a sequence of what?
-
(10 pts) In contrast to an “absolute pathname”, we have the concept of a “relative pathname”. What is the technical term used for the “starting point” of a “relative pathname”?
-
-
(10 pts) Section 4.2 discusses formatted output. What is the output of the following print statement? Please put one character per box to show the exact spacing. Try to figure it out by hand before checking your answer online. If you spoil the first grid, use the second.
print('{0:4},{1:6}'.format(12,345,6789))
-
Pages 112-114 discuss four ways of reading text from a file.
-
(10 pts) The first way is shown in the listing at the bottom of p. 112. On line 4 of that listing we see:
content = infile.read()
After this line of code is executed, what would
type(content)
return? (i.e. would it be<class 'int'>
,<class 'float'>
,<class 'str'>
,<class 'list'>
, or something else?) -
(10 pts) The second way is shown in the middle of p. 113. On line 7 of that listing we see:
wordList = content.split()
What does the
.split
method do, and what is stored inwordList
as a result? -
(10 pts) The third way is shown in the listing near the top of p. 114. On line 4 of that listing we see:
lineList = infile.readlines()
After this line of code is executed, what would
type(lineList)
return? (i.e. would it be<class 'int'>
,<class 'float'>
,<class 'str'>
,<class 'list'>
, or something else?) -
(10 pts) The fourth and final way is shown in an interactive example on the lower half of p. 114, and looks like this (with the Python prompts removed):
infile = open('example.txt') for line in infile: print(line,end='')
The book suggests that this fourth method has an advantage over the other three in a particular circumstance. What is the circumstance in which we would want to use this method instead of one of the other three?
-
-
(10 pts) Section 4.4 discusses two types of problems that can arise in a Python program: errors and exceptions. One difference is that in Python an error just results in a message bring printed on the screen, while an exception also results in an object being created that stores information about the exception. Another difference is the root cause, or context in which errors happen vs. exceptions. Describe the difference between what kind of problem causes an error, vs what kind of problem causes an exception.