1
h07
CS8 S18
Name:
(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 (Files), 4.4 (Errors and Exceptions)

ready? assigned due points
true Thu 04/19 03:30PM Thu 04/26 03:30PM

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.
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.)


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.

  1. (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class. It is important to fill in both name and umail every time, since handwriting is sometimes difficult to decipher. Having both helps us ensure you get credit for your work.

    Also: while we strongly prefer that you submit your homework on a single sheet of paper, if you MUST submit it on multiple sheets, JUST write your name at the top of both sheets and turn in both sheets UNCONNECTED.

    DO NOT staple, paper clip, spit-fold-and-tear, or do ANYTHING that would make it difficult to automatically feed your paper through a 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.

    print('{0:4},{1:6}'.format(12,345,6789))
    
  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:
         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?

  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.