VJC Chapter 6 File IO
Uploaded by cheesemuffin · 10 December 2025
Preview
Text from the first pagesVJC/H2Computing/9569 Chapter 6: File Input/Output (I/O) Contents 1. Introduction 2 2. Open and Close Files 2 2.1 Opening a non-existent file 3 2.2 Using the with method 3 3. Read from a file 3 3.1 Example: read() 4 3.2 Example: readline() 5 3.3 Example: readlines() 6 4. Writing and Appending to a File 6 4.1 Example: write() 6 4.2 Example: writelines() 7 5. Text File & Binary File 8 6. CSV File I/O 8 6.1 Python CSV Module 9 6.2 Example: csv.reader() 9 6.3 Example: Save result as a list 9 6.4 Example: Save header and data as two separate lists 11 6.5 Using csv.writer() 11 6.6 Example: writerow() and writerows() 11 7. JSON Processing 12 7.1 Example: dumps() 13 7.2 Example: More readable version 13 8. Types of files 14 8.1 Serial Files 14 8.2 Sequential Files 14 9. File Paths 14 9.1 Absolute Path 14 9.2 Relative Path 15 Referenced Readings 17
VJC/H2Computing/9569 Syllabus Learning Outcomes 2.3 Implementing Algorithms and Data Structures 2.3.4 Store data in and retrieve data from serial and sequential text files. 1. Introduction File is a named location on a disk to store related information. ● It uses non-volatile memory, e.g. hard disk, to store data permanently. ● A file can be in text or binary format. Advantages of using a file: ● It is more efficient to read large amounts of data from a file, compared to manual data entry. ● We are able to output the results to a file for ease of future reference. A file operation takes place in the following order: ● Open a file ● Perform operations ● Close the file 2. Open and Close Files Python has a built-in function open(file_path) to open a file. ● The open() function returns a file object, also called a file handler, as it is used to read or modify the file accordingly. To read data from a file object, use its read() method. To close a file object, use its close() method. It flushes any unwritten information and closes the file object, after which no more writing can be done. This prevents accidental access to an open file. Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file to release the resources.
VJC/H2Computing/9569 2.1 Opening a non-existent file It is important to ensure the file resides in the same folder as your Python notebook or file. Assuming test.txt is not in the same folder as the Python file, you will get similar error messages. 2.2 Using the with method Another way of opening a file is using the with method. This method automatically handles the closing of the file. 3. Read from a file There are 3 main modes when opening a file ● read from the file ● write to the file ● append to the file The default mode when calling open() is to read from the file. We can choose to include the default mode when opening the file, as a reminder to ourselves.
VJC/H2Computing/9569 There are 3 methods to read a file. Operator How it reads read() Reads the entire text as a single string readline() Reads the text line by line readlines() Read the entire text and returns a set of lines 3.1 Example: read() Make sure you have downloaded quote.txt in the same folder as your Python notebook or file.
VJC/H2Computing/9569 3.2 Example: readline() 3.3 Example: readlines() Often, we do not store the files in the same directory as the script or notebook. In this case, we have to make use of the os module or Path module. For the sake of simplicity, this will not be covered here but you are encouraged to find out how to use the above modules to access files in a different directory. A good starting point is to go to website link 7 in the Referenced Readings section of this document.
VJC/H2Computing/9569 4. Writing and Appending to a File When we want to write to a file, we open the file with the mode w. Things to note when using the write mode. ● If the file does not exist, a new file will be created. ● Warning: If the file exists, it will be overwritten. (All previous content will be removed.) ● write() is similar to print(), except it does not include newline character (hence the \n) ● Data is written as a text string. When using append mode, ● If the file does not exist, a new file will be created. ● If the file exists, new data will be added to the end of the string. ● As with write mode, use write(). ● Data is written as a text string. 4.1 Example: write() Complete the following operations using with code block. ● Write "Alexa, " to a file test.txt. This operator will overwrite any content in the file. ● Append "Good morning!\n" to the file test.txt. ● Append "What time is it?" to the file test.txt. ● Read and print out content from the file test.txt.
VJC/H2Computing/9569 4.2 Example: writelines() Similar to readlines(), writelines() will write a list of strings into the target file. Write the strings in text_list into test.txt.
VJC/H2Computing/9569 5. Text File & Binary File By default, open() assumes the file is a text file. To work with binary files, e.g. images and sound files, adding b to the mode. Examples of binary content include jpg files. ● Use rb to read a binary file ● Use wb to write binary content to a file 6. CSV File I/O Comma-Separated Values (CSV) is a common text file format to store tabular data. ● It has a simple structure where each row contains one record ● A record may have multiple attributes delimited by common delimiter such as , ● Normally, the first line is the header Sample CSV data: Why Use CSV? ● CSV is a common format for data exchange because it is simple and compact. ● Most relational databases provide tools to import and export CSV files. ● CSV files can be easily opened in Excel. 6.1 Python CSV Module While we could use the built-in open() function to work with CSV files in Python, there is a dedicated csv module that makes working with CSV files much easier. It contains following built-in functions: csv.reader csv.writer writerow() writerows()
VJC/H2Computing/9569 6.2 Example: csv.reader() Make sure you have the file info.csv in the same folder as the notebook. Print out all the rows in the file. 6.3 Example: Save result as a list
VJC/H2Computing/9569 6.4 Example: Save header and data as two separate lists 6.5 Using csv.writer() A csv.writer() can be used to write a CSV file. The csv.writer() function returns a writer object that converts the user's data into a delimited string and write to file using its writerow() function. The newline argument is set to “ when opening a file which the csv.writer() will write each row in a line. 6.6 Example: writerow() and writerows() Write the following data to a csv file.
Content continues in the PDF. Download PDF
Related notes
- NYJC 2026 Prelim P2Exam Papers · 2026
- NYJC 2026 Prelim P1Exam Papers · 2026
- DHS 2026 Y6 H2 Computing Prelim Paper 2_finalExam Papers · 2026
- ACJC 2026 JC2 Computing Prelim Paper 2 (Practical)Exam Papers · 2026
- 2026_NJC Prelim_Computing_P2.pdfExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P2_finalExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_markschemeExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_finalExam Papers · 2026
- 2026 ACJC Prelim Computing Paper 2Exam Papers · 2026
- 2024 ACJC Computing PromoExam Papers · 2024
- 2023 ACJC Promo QPExam Papers · 2023
- 2022 ACJC Computing Promo Paper 2Exam Papers · 2022
- See all H2 Computing notes

