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

  1. What are the two things that uniquely specify a file?
    name and location
  2. 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")
  3. 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")
  4. 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")
  5. What happens when you use the append access mode on a file?
    adds text to the bottom of the file
  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. What is the empty string?
    a string with no characters in it
  8. What does readline() return when it gets to the end of the file?
    the empty string
  9. What must you do if you want to write an integer to a text file?
    convert it into a string
  10. 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
  11. 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())