IT 116: Introduction to Scripting
Answers to Class 18 Ungraded Quiz

  1. Write the Python statement that would create a file object for the file scores.txt for reading and assign it to the variable scores_file.
    scores_file = open("scores.txt", "r")
  2. Write the Python statement that would create a file object for the file scores.txt for writing and assign it to the variable scores_file.
    scores_file = open("scores.txt", "w")
  3. Write the Python statement that would create a file object for the file scores.txt for appending and assign it to the variable scores_file.
    scores_file = open("scores.txt", "a")
  4. If you had the file variable scores_file and you wanted to get all the data in the file and store it in the variable results what Python statement would you write?
    results = scores_file.read()
  5. 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()
  6. 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")
  7. 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
  8. 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
  9. What does the append access mode do to a file?
    adds to the bottom of the file
  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)