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. What happens when you use the append access mode on a file?
    adds text to the bottom of the file
  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. What is the empty string?
    a string with no characters in it
  7. What does readline() return when it gets to the end of the file?
    the empty string
  8. What must you do if you want to write an integer to a text file?
    convert it into a string
  9. What must you do if you read "87" from a text file and want to add it to another number?
    conver it to an integer
  10. Write a for loop that reads and prints a file. Assume you have already created a file object and have stored a reference to it in the variable file.
    for line in file:
    	print(line.rstrip())