Edexcel IGCSE Computer Science 4CP0
Algorithms
Searching and Sorting Algorithms
Decomposition and Abstraction
Developing and Testing Code
Programming Constructs and Operators
Data Types and Data Structures
Input, Output and Validation
Data Representation
Data Storage and Compression
Encryption
Hardware and the CPU
Software
Programming Languages and Translators
Networks
Network Security
The Internet and the World Wide Web
Emerging Trends, Issues and Impact
Input, Output and Validation
Text files
- 3 things to know
- Papers 1 and 2
Overview
Reading and writing text files in Python and pseudocode, write and append modes, splitting lines and file errors.
What you need to know
Answer each one before you reveal it.
How does the pseudocode command set read from and write to a file?
What are the steps in writing to a text file?
Questions from this subtopic
Each one with the model answer underneath.
- 1
Why do programs read and write text files?
Model answer
Data in variables is lost when a program closes. Writing to a file stores the data permanently so it can be read back later.
- 2
What are the steps in reading a text file?
Model answer
The program opens the file in read mode. The program reads the lines one at a time. The program then closes the file.
- 3
What are the steps in writing to a text file?
Model answer
The program opens the file in write or append mode and writes the data. The program then closes the file.
- 4
What is the difference between write mode and append mode?
Model answer
Write mode replaces the whole contents of the file. Append mode adds new data to the end of the existing contents.
- 5
Why should a file be closed after use?
Model answer
Closing the file saves any data still waiting to be written. Other programs can then use the file.
- 6
Write Python code that reads every line of scores.txt and prints it.
Model answer
The code opens the file with file = open('scores.txt', 'r'), then for line in file: print(line.strip()), then file.close().
Other ways to work this
The same content, four other ways.
Everything in your subscription
Runs, plus a mode for every other kind of session.
Every question in Input, Output and Validation, on a schedule.
Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.
Or £46 a year, about £3.83 a month. Cancel anytime.