Topic Two (1.2): File Organisation and Access
Introduction
In the AS course you learnt about text files. Text files are superb in that they allow you to go and look at the data, and change it if necessary, in a plain text viewer such as Notepad, Atom, or Nano. There are a few problems with this, however. Firstly, anyone can go and look at the data and potentially edit it! Even more importantly, not everything can be stored as text. Text files use ASCII, Unicode, or similar character encodings. Images have data which do not have equivalent character codes. So do sounds, videos, and various other file formats. There are other problems as well which, the more you use text files, the more you will discover.
The alternative is to store files as binary files. Now, yes, text files are stored in binary as well, but when we refer to binary files, we are referring to a file which is structured in such a way that it can be stored in its internal binary representation.
In this topic you will explore types of binary files (including serial, sequential and direct access) and look at the advantages and disadvantages each has over the other.
Watch
WATCH - Python 16 - Binary Files (YouTube) Links to an external site. This video walks you through reading and writing a binary file in Python.
WATCH - Data File Formats: 01 - What is Binary & Bytes (YouTube) Links to an external site. This video gives some further detail about the way binary data is used in binary files.
WATCH - Serial Access and Direct Access (YouTube) Links to an external site. This video details serial vs direct access in more detail. Note that this is about file access theory, not about implementation in a programming language.
Read and Make Notes
Read pages 317 to 320 of your textbook. Complete the questions and tasks within the text as you go.
CHECK – Module 1 Textbook Answers Download CHECK – Module 1 Textbook Answers
Tasks
Complete the following:
- Summarise in your own words the difference between serial, sequential and direct-access files. Try and think of at least two examples of when each type would be suitable for use in a computer program.
Further Reading
READ - Python – How to Read and Write Files (Python Guru) Links to an external site. The Python Guru has an excellent page on reading and writing files, from the very basics to some quite advanced techniques.
Extended Learning
READ - Python Cookbook (O’Reilly) Links to an external site. Direct Access isn’t as simple as it could be. This O’Reilly page takes you through some of the concepts that could be used.
End of Topic 2 Review
Complete exam-style question 2 on page 326.
CHECK – Module 1 Exam-style Answers Download CHECK – Module 1 Exam-style Answers
Top Tips
Ensure that you understand the following before moving on to the next topic.
- File organisation can have a huge impact on efficiency.
- Binary file storage can take up a lot less space than text file storage.
- Binary file formats are not (easily) read by a human being.
Key Terms
Add the following subject vocabulary to your glossary. Write the definitions in your own words to embed understanding.
- Binary file
- Serial file
- Sequential file
- Direct-access file
Consolidate
Extend your understanding by working through these additional resources/tasks:
- Consider the books program from the previous topic.
- Add functionality to write the book data to a binary file.
- What file organisation method did you choose?
- Extend the program to allow book data to be read from a binary file.
- Can you include direct access?