Plain text (TXT) files: structure

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Plain text (TXT) files: structure

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement best describes a plain text (TXT) file?
A It stores unformatted characters, usually one item per line
B It stores numbers, dates and strings as separate typed fields
2. How does a TXT file usually mark the end of each line of text?
A A closing tag such as </line>
B A newline character
3. A TXT file contains the line 42. In Python, what type is the value after reading it with readline()?
A An integer
B A string
4. Which is true about field headings in a plain TXT file?
A There are no built-in field headings
B The first line is always treated as the field headings
5. A program reads the line 15 from scores.txt and needs to add 5 to it. Which code is correct?
A total = int(line) + 5
B total = line + 5

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Ava\nLiam\nMia AA TXT file has no fields or data types, so it cannot store typed fields with headings. It only holds plain characters, usually one line per item.
2line = file.readline() # reads 88 Bmark = int(file.readline())
3A TXT file has no field headings, so the file does not label what each line means CAva\n88\nLiam\n65
4Because everything in a TXT file is read as a string, a number must be converted before it is used in a calculation DA TXT file where each line holds one item, and the end of each line is marked by a newline character
5total = file.readline() + 10 EThe line read from a TXT file is a string, so adding the integer 10 causes a type error. Convert it with int() first.
6Name: Ava, Mark: 88 stored in a TXT file as typed fields FThe value read from a TXT file is the string '88', not the integer 88

Definitions

TXT file

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
f = open("names.txt", "w") f.write("Ava__")
2.
line = f.readline() age = ___(line)
3.
f = open("names.txt") lines = f._________()
4.
line = f.readline() name = line._____()
5.
line = f.readline() print(type(line) == ___)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A f = open("scores.txt")
B line = f.readline()
C total = line + 5
D print(total)
E f.close()
Question 2
A f = open("names.txt", "r")
B names = f.readlines
C print(len(names))
D f.close()
Question 3
A with open("notes.txt") as f
B text = f.read()
C print(text)
Question 4
A f = open("log.txt", "w")
B f.write("Login ok/n")
C f.write("Logout\n")
D f.close()
Question 5
A f = open("names.txt")
B for line in f:
C name = line.trim()
D print("Hi " + name)

Answer Key

Plain text (TXT) files: structure
Section A: Quick Questions
Q1
A — It stores unformatted characters, usually one item per line
Q2
B — A newline character
Q3
B — A string
Q4
A — There are no built-in field headings
Q5
A — total = int(line) + 5
Section B: Match the Pairs
Q1
Ava\nLiam\nMia → A TXT file where each line holds one item, and the end of each line is marked by a newline character
Q2
line = file.readline() # reads 88 → The value read from a TXT file is the string '88', not the integer 88
Q3
A TXT file has no field headings, so the file does not label what each line means → Ava\n88\nLiam\n65
Q4
Because everything in a TXT file is read as a string, a number must be converted before it is used in a calculation → mark = int(file.readline())
Q5
total = file.readline() + 10 → The line read from a TXT file is a string, so adding the integer 10 causes a type error. Convert it with int() first.
Q6
Name: Ava, Mark: 88 stored in a TXT file as typed fields → A TXT file has no fields or data types, so it cannot store typed fields with headings. It only holds plain characters, usually one line per item.
Section C: Definitions
Q1
TXT file — A plain text file holds unformatted characters, usually one line of text per line ended by a newline. It has no field headings or data types, so everything is read as a string.
Section D: Fill in the Blanks
Q1
f = open("names.txt", "w") f.write("Ava\n")
Q2
line = f.readline() age = int(line)
Q3
f = open("names.txt") lines = f.readlines()
Q4
line = f.readline() name = line.strip()
Q5
line = f.readline() print(type(line) == str)
Section E: Spot the Error
Q1
C — Everything read from a text file is a string, so line + 5 mixes a string and an integer and causes a TypeError. Convert it first: total = int(line) + 5.
Q2
B — readlines needs brackets to be called. Without them names holds the method itself, not the list of lines. It should be names = f.readlines().
Q3
A — A with statement must end with a colon so the indented block below it belongs to it. It should be with open("notes.txt") as f:
Q4
B — A newline character is written \n with a backslash, not /n. With /n the text ends in the characters /n instead of ending the line. It should be f.write("Login ok\n").
Q5
C — Python strings have no trim() method. Use strip() to remove the newline at the end of each line. It should be name = line.strip().