← VCE Software Development U3O1

Plain text (TXT) files: structure

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.

Understanding Plain text (TXT) files: structure in VCE Software Development U3O1

What it does: Nothing in the file says that the lines are names and marks. The program must know the layout itself. int() converts the string read from the file into an integer. A TXT file stores no data types. Lines in a TXT file end with a newline character (\n). Closing tags belong to XML. line is a string, so line + 5 causes a type mismatch error. Convert it with int() first. A .txt file is plain text. XML files use tags to give the data structure. A TXT file usually stores one item of text per line, and each line is ended by a newline.

Common mistakes: 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. readlines needs brackets to be called. Without them names holds the method itself, not the list of lines. It should be names = f.readlines().

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 98–113) and word search (Modules 72–113).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which statement best describes a plain text (TXT) file?
It stores unformatted characters, usually one item per lineIt stores numbers, dates and strings as separate typed fields
Matches
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.
Applying
Meanings
Term
TXT file
Meaning
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.
Practice
Ava Nguyen
Testing
Gaps
f.write("Ava")
Check Answer
Spots
2line = f.readline()
3total = line + 5

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Hyena' badge — Savannah Debugger, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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
A TXT file is just characters. It has no built-in fields or data types.
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 ✓
Lines in a TXT file end with a newline character (\n). Closing tags belong to XML.
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 ✓
Everything read from a TXT file is a string. You must convert it with int() if you need a number.
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
A TXT file has no structure for headings. Any heading is just another line of text that your program must handle.
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
line is a string, so line + 5 causes a type mismatch error. Convert it with int() first.

Match the Pairs

Ava\nLiam\nMia → A TXT file where each line holds one item, and the end of each line is marked by a newline character
line = file.readline() # reads 88 → The value read from a TXT file is the string '88', not the integer 88
A TXT file has no field headings, so the file does not label what each line means → Ava\n88\nLiam\n65
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())
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.
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.

Key Term

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.

Fill in the Blanks

1. f = open("names.txt", "w") f.write("Ava\n")
Hint: Type the escape sequence that ends a line of text in the file.
2. line = f.readline() age = int(line)
Hint: Type the function that converts the text read from the file into a whole number.
3. f = open("names.txt") lines = f.readlines()
Hint: Type the file method that returns every line of the file as a list.
4. line = f.readline() name = line.strip()
Hint: Type the string method that removes the newline character from the end of a line.
5. line = f.readline() print(type(line) == str)
Hint: Type the data type that every value read from a text file has.

Spot the Error

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

Student with a class code? Join your class →