Files: Cambridge IGCSE practice
Reading and writing text files. 11 questions in the style of the Cambridge IGCSE exam.
Sample questions
Which file mode adds new data to the end of an existing text file without deleting what is already there?
- "r"
- "w"
- "a"
- "x"
What does this program print?
with open("scores.csv", "w") as f:
f.write("Amy,72\nBen,58\nCal,91\n")
best = ""
top = 0
with open("scores.csv") as f:
for line in f:
parts = line.strip().split(",")
if int(parts[1]) > top:
top = int(parts[1])
best = parts[0]
print(best, top)A program runs OPENFILE "Scores.txt" FOR WRITE. The file already contains data. What happens?
- A new file is created and the existing data in Scores.txt is lost
- New data is added to the end of the existing data
- The program can now read and write the file at the same time
- An error occurs because the file already exists