A scanner stores numeric tags in tags.txt. The file contains exactly the nine characters represented by the Python string literal "23\n57\n81\n"; each \n represents one newline character.
The file position is the location where the next read begins. Run the following Python code mentally, assuming all operations succeed:
with open("tags.txt", "r", encoding="utf-8") as f: a = f.read(2) b = f.readline() c = f.read(4) d = f.read() e = f.read() f.seek(0) g = f.readline()
Find the number of characters in b, the number of characters in e, and the total number of characters returned across a, b, c, d, e and g. Count repeated characters again when they are returned again. Give each answer as an exact integer in characters.
Unlock full course notes by signing up
Capabilities of the r, w and a file modes
In Python, the built-in open() function takes a mode string that controls what a program may do with a text file. For each mode below, used on its own as the mode argument, complete the table by typing yes or no in every cell.
Average of readings from a file with a blank line
A rain gauge logger saves daily rainfall in millimetres to a file named rain.txt, one reading per line. A power glitch left the third line empty. Every line, including the empty one, ends with a newline character. The file contents are:
The following Python program processes the file:
What value does the program print? Give the value in millimetres, to two decimal places.
Matching text-file methods to their results
A file object is the object a Python program uses to access an open file. Match each text-file method call to its behaviour. Assume the file is open in a mode that permits the operation, and no operation raises an exception.
Line and character counts after mixed writes
A program starts with no file named out.txt on the disk and runs the Python code below. Each newline character counts as one character.
After the code finishes, find the value of len(lines), the total number of characters stored in out.txt, and the value of len(lines[0]). Give each as a whole number.