Reading and Writing Files, Including CSV · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Files that outlive your program

Computing · Programming · ages 17-18
Name ______________________   Date ____________
  1. Which call grabs just the next line from an open file?

    • readline
    • read
    • write
  2. Which call hands you a file object handle to work through?

    • read
    • open
    • close
  3. Which call returns every line as a list of strings?

    • readline
    • read
    • readlines
  4. You open just data.txt with no folder. What kind of path is that?

    • A relative path
    • An absolute path
    • A web address
  5. write ran with no errors, yet the file looks empty. What was missed?

    • Opening the file
    • Closing the file
    • Naming the variable
  6. What is the shape of a CSV file?

    • Rows and columns split by commas
    • One long word with no breaks
    • Pictures stored as pixels
  7. A read line ends with a newline and your compare fails. What is the repair?

    • Add more files
    • Strip the newline before comparing
    • Rename the variable
  8. A student prints the file object expecting its text but sees object details. Why?

    • Printing shows the handle, not the contents
    • The file deleted itself
    • Text files hold no strings
LightMySky · lightmysky.comW1-mt_j5qb4FdkdI-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Files that outlive your program W1-mt_j5qb4FdkdI-s1

  1. readline · Readline returns one line at a time.
  2. open · Open returns the handle every other step uses.
  3. readlines · Readlines collects all lines into a list.
  4. A relative path · No folder means a relative name, resolved where you run.
  5. Closing the file · Buffering only flushes to disk on close.
  6. Rows and columns split by commas · Each line is a record and commas split its fields.
  7. Strip the newline before comparing · Read lines carry their line ending along.
  8. Printing shows the handle, not the contents · You must read through the handle to see text.
Worksheet · LightMySky