Resources | Subject Notes | Computer Science
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.
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.
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.
Once a file is open, you can read data from it using functions like read()
and readline()
.
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()
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.
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()
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.
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.
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.