IT 116: Introduction to Scripting
Quiz 7 Answers

  1. What are the two things that uniquely specify a file?
    name and location
  2. What must you create to work with a file in Python?
    a file object
  3. What is the Python function that creates the thing you need to work with a file?
    open
  4. What two things does this function do?
    creates a file object and returns a reference to it
  5. What two pieces of information do you need to give this function as arguments?
    pathname and access mode
  6. If you wanted to read a single line of a file for which you had the file variable scores_file and store it in the variable line what Python statement would you write?
    line = scores_file.readline()
  7. If you wanted to write "78" to a file for which you had the file variable scores_file what Python statement would you write?
    scores_file.write("78")
  8. What does the method used to return a single line of a file return when it it reaches the end of the file?
    the empty string
  9. What must you do if you have read the entire contents of a file and now want to read it again?
    close the file and open it again for reading
  10. Write a for loop which reads in integers stored in a file to which you have read access through the scores_file and adds them to the accumulator variable total.
    for line in scores_file:
        total += int(line)