Files and databases · GCSE · OCR J277 2.2.3, Edexcel 1CP2 6.4.2 · about 15 min
Open, read, write, append and close: a run log in a CSV file.
[1 mark]Put the steps for reading a file in order.
Number the lines 1 to 3 to put them in the right order.
Open the fileRead from the fileClose the file[1 mark]What does opening a file with mode "w" do to a file that already exists?
[1 mark]Which mode adds new lines to the end of a file and keeps what was there?
[1 mark]What does this program print?
line = "3,square,80,9.4\n"
fields = line.strip().split(",")
print(fields[1], float(fields[2]) + 1)[1 mark]Why use with open(...) as f:?
[1 mark]Why store data in a file rather than only in variables?
Drive forward 25 cm, timing it with clock(). Then append a line to runs.csv for this run: run number 4, task wall, 25 cm, and the seconds rounded to one decimal place, in the same format as the other lines. Finally read the file and print 4 runs logged, counting the lines after the header rather than typing 4.
The task starts from the original four-line runs.csv every time it runs, whatever earlier runs added to the copy above.
# the two lines every program starts with: the commands, then the robot from bugbot import * connect() start = clock() forward(50, distance=25)
Plan your program here, then type it in and press Run.
runs.csv if you use mode "w" instead of "a"? Try it, then reset the file.