Open, read, write and close files

Resources | Subject Notes | Computer Science

IGCSE Computer Science - File Handling

File Handling

This section covers the fundamental operations involved in working with files in a computer program. We will explore how to open files, read data from them, write data to them, and properly close them to ensure data integrity.

Opening Files

Before you can work with a file, you need to open it. The open() function is used for this purpose. It takes two arguments: the file name (as a string) and the mode in which you want to open the file.

File Opening Modes

Different modes specify how the file should be opened. Here's a table summarizing common modes:

Mode Description
'r' Read mode. Opens the file for reading. The file must exist.
'w' Write mode. Opens the file for writing. If the file exists, its contents are overwritten. If the file doesn't exist, a new file is created.
'a' Append mode. Opens the file for writing. New data is added to the end of the file. If the file doesn't exist, a new file is created.
'x' Exclusive creation mode. Opens a file for exclusive creation. If the file already exists, the operation fails.
'r+' Read and write mode. Opens the file for both reading and writing.
'w+' Read and write mode. Opens the file for both reading and writing. If the file exists, its contents are overwritten. If the file doesn't exist, a new file is created.
'a+' Read and append mode. Opens the file for both reading and appending. New data is added to the end of the file. If the file doesn't exist, a new file is created.

It's crucial to specify the correct mode to avoid unintended data loss or errors.

Reading from Files

Once a file is open, you can read data from it using functions like read() and readline().

Reading Data

The read() function reads a specified number of characters from the file. The readline() function reads a single line from the file.

Example:

file = open("my_file.txt", "r")
data = file.read()  # Reads the entire file content
print(data)
file.close()

Writing to Files

To write data to a file, you use the write() function. This function takes a string as an argument and writes it to the file.

Writing Data

The write() function can be used to write strings to the file. If the file is opened in write mode ('w'), any existing content will be overwritten.

Example:

file = open("output.txt", "w")
file.write("This is some text to write to the file.\n")
file.write("Another line of text.\n")
file.close()

Closing Files

It's essential to close files after you're finished with them using the close() function. This releases the resources held by the file and ensures that any buffered data is written to the disk.

Failure to close files can lead to data loss or other problems.

Example Program

Here's a simple example that demonstrates opening, reading, and closing a file:

file = open("example.txt", "r")
content = file.read()
print(content)
file.close()

This program opens the file "example.txt" in read mode, reads its entire content into the variable `content`, prints the content to the console, and then closes the file.

Error Handling

When working with files, it's important to consider potential errors, such as the file not existing or not having the necessary permissions. You can use error handling mechanisms (like try...except blocks) to gracefully handle these situations.

Suggested diagram: A flowchart illustrating the process of opening, reading, and closing a file, with error handling steps.