CS8 F17
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
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, 4.4) Files, Errors, Exceptions

ready? assigned due points
true Tue 10/24 08:00AM Tue 10/31 08:00AM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

There is NO MAKEUP for missed assignments, and you may not submit work in advance, or on behalf of another person.
In place of that, we drop the four lowest scores (if you have zeros, those are the four lowest scores.)


Please read Perkovic (4.3, 4.4) Files, Errors, Exceptions. Then complete these problems and turn in your completed homework during your registered lab section..

  1. (10 pts) Please fill in the information at the top of this homework sheet, as usual. WRITE DARK, and remember, if you MUST submit it on multiple sheets, JUST write your name at the top of both sheets and turn in both sheets UNCONNECTED. No staples, paper clips, fold/tear etc or anything that would jam up the scanner.

  2. One important abstraction provided by most operating systems (Windows, Linux, or MacOS, for example) is something called a “file system”.

    1. (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?

    2. (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?

    3. (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”?

  3. (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.

  4. Pages 112-114 discuss four ways of reading text from a file.

    1. (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?)

    2. (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 in wordList as a result?

    3. (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?)

    4. (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:

      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?

  5. (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.