Python Programming · High School (11–12) · 1 course, 11 worked problems, 28 practice questions
A program loses its variables when it stops, so data that must be kept goes into files. In Python you open a file with open(), usually inside a with block so that it closes safely. You can read it line by line, write to it, or append new lines. Many files follow a standard format. A CSV file stores a table as lines of values separated by commas, and the csv module reads it. A JSON file stores nested data, such as dictionaries and lists, and the json module turns it into Python objects. These formats let programs share data.
open() and withChoose an answer, then check it. Each one shows a full explanation.
A program writes to a file inside a with block, then uses the file object after the block.
with open("a.txt", "w") as f:
print(f.closed, end=" ")
f.write("hi")
print(f.closed, f.name)
f.write("again")
What happens when the program runs?
A class adds lines to a text file, and reads the file back. The folder has no file log.txt at the start.
class Log:
def __init__(self, path):
self.path = path
def add(self, text):
with open(self.path, "a") as f:
f.write(text + "\n")
def lines(self):
with open(self.path) as f:
return f.read().split("\n")
log = Log("log.txt")
log.add("start")
log.add("stop")
print(log.lines())
What does the program print?
A program writes three bytes to a binary file, then reads them into a bytearray.
data = bytearray(b"ABCDEF")
with open("bin.dat", "wb") as f:
f.write(data[1:4])
buf = bytearray(5)
with open("bin.dat", "rb") as f:
n = f.readinto(buf)
print(n, buf[:n].decode(), buf[3], len(buf))
What does the program print?
A program writes to a new file with write and with print, then reads the whole file back. It prints nothing on the screen.
with open("out.txt", "w") as f:
n = f.write("ab\n")
print("cd", "ef", sep="-", file=f)
with open("out.txt") as f:
text = f.read()
Which of these give True after the code? Choose every correct option.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
The file words.txt holds three lines, red, green and blue, each with a new line at its end. A program reads part of it with read, then loops over the rest.
with open("words.txt") as f:
start = f.read(5)
for line in f:
print(line.strip().upper(), end=" ")
print(repr(start))
What does the program print?
A program writes three lines to a file, then adds up the lines that are numbers. A line that is not a number counts as 100.
with open("n.txt", "w") as f:
f.write("3\nx\n5\n")
total = 0
with open("n.txt") as f:
for row in f:
try:
total += int(row)
except ValueError:
total += 100
print(total, f.closed)
What does the program print?
A program writes a file in two steps, then reads it back. with open("log.txt", "w") as f: f.write("one\ntwo\n") with open("log.txt", "a") as f: f.write("three") with open("log.txt") as f: first = f.readline() rest = f.readlines() print(repr(first), rest, len(rest)) What does the program print?
A folder holds a file notes.txt with the text hi. It has no file new.txt. Each line below is run on its own, in a fresh copy of the folder.
Which lines run without raising an exception? Choose every correct option.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
A program opens notes.txt in mode "w" and calls f.write("hi\n") three times. How many lines does the file have?
What is "Asha:72".split(":")[1], and what type is it?
Each one has a hint, the key idea and a full worked solution.