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. total starts at 0. A loop reads one integer per text-file line. Which update adds every line's number to the running total?

    • total += int(line)
    • total = int(line)
    • total += 1
  2. notes.txt exists. Which expression opens it for reading and returns a file object?

    • f.read('notes.txt')
    • open('notes.txt', 'r')
    • f.close('notes.txt')
  3. A text file contains '9\n7\n4\n'. f.readline() has consumed the first line. What does f.readlines() return now?

    • ['9\n', '7\n', '4\n']
    • '7\n4\n'
    • ['7\n', '4\n']
  4. The script is in /projects/app, but its current working directory is /projects/run. It opens the relative path data.txt. Which directory supplies the path's starting point?

    • /projects/run, the current working directory
    • /projects/app, the directory containing the script
    • The filesystem root, as for an absolute path
  5. A writable file f remains open with output buffered. All operations succeed. Which action both flushes that output and releases the file resource?

    • f.flush()
    • f.close()
    • f.write('')
  6. With a comma delimiter and standard CSV quoting, which fields does csv.reader return for the record '"Maple, gold",14'?

    • ['Maple, gold', '14']
    • ['Maple', ' gold', '14']
    • ['"Maple, gold"', '14']
  7. line contains ' cedar\n'. The target is ' cedar', including its leading space. Which expression removes only the trailing newline for this comparison?

    • line.strip(' \n')
    • line.rstrip('\n')
    • line.replace('\n', ' ')
  8. csv.DictReader returns row['year'] as the string '2031'. Which assignment reconstructs that field as the integer 2031?

    • row['year'] = int(row['year'])
    • row['year'] = str(row['year'])
    • row['year'] = len(row['year'])
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. total += int(line) · Convert the line to an integer, then add it to the existing total. Assignment alone replaces the total; adding 1 counts lines.
  2. open('notes.txt', 'r') · open takes the path and mode and returns the file object. read and close act on an existing object.
  3. ['7\n', '4\n'] · The first line is already consumed. readlines returns the remaining two lines as a list, retaining their newlines.
  4. /projects/run, the current working directory · Relative paths start from the process's current working directory, here /projects/run.
  5. f.close() · close flushes buffered output and closes the resource. flush alone leaves it open.
  6. ['Maple, gold', '14'] · The quoted comma belongs inside the first field. The surrounding CSV quotes are removed, and both returned fields are strings.
  7. line.rstrip('\n') · rstrip('\n') removes trailing newlines while keeping the leading space. The other choices remove that space or add a trailing space.
  8. row['year'] = int(row['year']) · int converts the numeric text to 2031. str keeps it as text, while len returns the character count 4.
Worksheet · LightMySky