1
h07
CCS CS20 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 05/24 12:30PM Thu 05/31 12:30PM

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

This assignment should be submitted by scanning the pages in the correct order to a PDF file and uploading to gradescope.com.

For more information, visit ucsb-cs8.github.io and look for Gradescope: Student Self Submission under "topics".

Even though it is a Gradescope submission, nevertheless, *please fill in the information at the top of this homework sheet*, including your name and umail address.


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) These 10 points are for filling in your name at the top of the sheet, and scanning correctly on Gradescope. (Having these here gives me a place to give you feedback if there is some glitch with the way you are doing it.)

  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.