2.4.4
File Handling
Test your knowledge with free interactive questions on Seneca — used by over 10 million students.
File Handling
We must be careful with the way we access files so that we do not accidentally corrupt them.

Open the file
- First we open the file for use, so that the operating system knows that we are using it.
- We can do this using:
- openRead("filename.txt")
- openWrite("filename.txt")

Assign the file to a variable
- The file must be assigned to a variable so that we can keep refering to it.
- This can be done as follows:
- myFile = openRead("filename.txt")
- myFile = openWrite("filename.txt")

Read / write
- We can now read or write to the file using the following commands:
- myFile.readLine() will return the next line of a file as a string.
- myFile.writeLine(
) will write a line to the file. - myFile.endOfFile() will return True if we reach the end of the file.

Close the file
- Finally, we must close the file so that it is free for another program to access.
- We can do this using:
- myFile.close()
1Problem Solving
1.1Algorithms
1.2Decomposition & Abstraction
2Programming
2.1Develop Code
2.2Constructs
2.3Data Types & Structures
2.6Subprograms
3Data
3.2Data Representation
3.3Data Storage & Compression
3.4Encryption
4Computers
4.1Machines & Computational Modelling
4.2Hardware
4.2.1Elements of Computer Systems4.2.2Types of Computer Systems4.2.3Memory - RAM4.2.4Memory - ROM4.2.5Memory - Cache4.2.6Running Out of Memory4.2.7Virtual Memory4.2.8Magnetic Storage4.2.9Properties of Magnetic Media4.2.10Examples of Magnetic Storage4.2.11Solid State Storage4.2.12Properties of Solid State Media4.2.13Optical Storage4.2.14Properties of Optical Storage4.2.15Examples of Optical Storage4.2.16Types of Optical Disk4.2.17Von Neumann Architecture4.2.18Registers of the Central Processing Unit (CPU)4.2.19Fetch-Decode-Execute Cycle4.2.20Factors Affecting CPU Performance4.2.21The Cloud4.2.22How the Cloud Works4.2.23The Cloud - Pros4.2.24The Cloud - Cons4.2.25End of Topic Test - Hardware
4.4Software
5Communication & The Internet
5.1Networks
5.1.1Benefits of Networks5.1.2Network Performance5.1.3Types of Network5.1.4Client-Server Model5.1.5Pros of Client-Server Model5.1.6Cons of Client-Server Model5.1.7Peer-to-Peer Model5.1.8Pros of Peer-to-Peer Model5.1.9Cons of Peer-to-Peer Model5.1.10Network Hardware5.1.11Transmission Media5.1.12WiFi5.1.13WiFi Frequency and Channels5.1.14WiFi Encryption5.1.15Network Protocols5.1.16Transmission Protocols5.1.17Web Protocols5.1.18Email Protocols5.1.19Layers5.1.20TCP and OSI Models5.1.21Advantages of Layering5.1.22Topology - Star5.1.23Topology - Mesh5.1.24Topology - Ring5.1.25Topology - Bus5.1.26End of Topic Test - Networks
5.2Network Security
6The Bigger Picture
6.1Emerging Trends, Issues & Impact
6.1.1E-Waste6.1.2Energy Consumption6.1.3Positive Environmental Impact6.1.4Ethical Issues - The Digital Divide6.1.5Ethical Issues - Net Neutrality6.1.6Ethical Issues - Working Conditions6.1.7Ethical Issues - Censorship6.1.8Online Activity Tracking6.1.9The Internet of Things6.1.10Positive Cultural Impacts6.1.11Negative Cultural Impacts6.1.12Data Protection Act6.1.13Computer Misuse Act6.1.14Copyright Designs and Parents Act6.1.15Creative Commons Licensing6.1.16Freedom of Information Act6.1.17Open Source Software6.1.18Proprietary Software6.1.19Licensing Issues6.1.20End of Topic Test - Software & Issues
Jump to other topics
1Problem Solving
1.1Algorithms
1.2Decomposition & Abstraction
2Programming
2.1Develop Code
2.2Constructs
2.3Data Types & Structures
2.6Subprograms
3Data
3.2Data Representation
3.3Data Storage & Compression
3.4Encryption
4Computers
4.1Machines & Computational Modelling
4.2Hardware
4.2.1Elements of Computer Systems4.2.2Types of Computer Systems4.2.3Memory - RAM4.2.4Memory - ROM4.2.5Memory - Cache4.2.6Running Out of Memory4.2.7Virtual Memory4.2.8Magnetic Storage4.2.9Properties of Magnetic Media4.2.10Examples of Magnetic Storage4.2.11Solid State Storage4.2.12Properties of Solid State Media4.2.13Optical Storage4.2.14Properties of Optical Storage4.2.15Examples of Optical Storage4.2.16Types of Optical Disk4.2.17Von Neumann Architecture4.2.18Registers of the Central Processing Unit (CPU)4.2.19Fetch-Decode-Execute Cycle4.2.20Factors Affecting CPU Performance4.2.21The Cloud4.2.22How the Cloud Works4.2.23The Cloud - Pros4.2.24The Cloud - Cons4.2.25End of Topic Test - Hardware
4.4Software
5Communication & The Internet
5.1Networks
5.1.1Benefits of Networks5.1.2Network Performance5.1.3Types of Network5.1.4Client-Server Model5.1.5Pros of Client-Server Model5.1.6Cons of Client-Server Model5.1.7Peer-to-Peer Model5.1.8Pros of Peer-to-Peer Model5.1.9Cons of Peer-to-Peer Model5.1.10Network Hardware5.1.11Transmission Media5.1.12WiFi5.1.13WiFi Frequency and Channels5.1.14WiFi Encryption5.1.15Network Protocols5.1.16Transmission Protocols5.1.17Web Protocols5.1.18Email Protocols5.1.19Layers5.1.20TCP and OSI Models5.1.21Advantages of Layering5.1.22Topology - Star5.1.23Topology - Mesh5.1.24Topology - Ring5.1.25Topology - Bus5.1.26End of Topic Test - Networks
5.2Network Security
6The Bigger Picture
6.1Emerging Trends, Issues & Impact
6.1.1E-Waste6.1.2Energy Consumption6.1.3Positive Environmental Impact6.1.4Ethical Issues - The Digital Divide6.1.5Ethical Issues - Net Neutrality6.1.6Ethical Issues - Working Conditions6.1.7Ethical Issues - Censorship6.1.8Online Activity Tracking6.1.9The Internet of Things6.1.10Positive Cultural Impacts6.1.11Negative Cultural Impacts6.1.12Data Protection Act6.1.13Computer Misuse Act6.1.14Copyright Designs and Parents Act6.1.15Creative Commons Licensing6.1.16Freedom of Information Act6.1.17Open Source Software6.1.18Proprietary Software6.1.19Licensing Issues6.1.20End of Topic Test - Software & Issues
Practice questions on File Handling
Can you answer these? Test yourself with free interactive practice on Seneca — used by over 10 million students.
- 1File HandlingPut in order
- 2Which of these are valid file handling methods?True / false
- 3
- 4Why must we close a file?Multiple choice
- 5What is the purpose of the openRead() pseudocode command? Multiple choice
Unlock your full potential with Seneca Premium
Unlimited access to 10,000+ open-ended exam questions
Mini-mock exams based on your study history
Unlock 800+ premium courses & e-books