3
Lesson 3
Opening and closing files
Objective
By the end of this lesson, students will understand how to open and close files in Python using the open() function. They will learn about different modes for opening files, including read, write, and append, and the distinction between binary and text modes.
1. Introduction to the open() Function:
The open() function in Python is used to open files for various operations such as reading, writing, and appending data. The basic syntax is:
By the end of this lesson, students will understand how to open and close files in Python using the open() function. They will learn about different modes for opening files, including read, write, and append, and the distinction between binary and text modes.
1. Introduction to the open() Function:
The open() function in Python is used to open files for various operations such as reading, writing, and appending data. The basic syntax is:
file = open("filename", "mode")
Here, "filename" specifies the file's name or path, and "mode" determines how the file will be accessed.
2. Modes for Opening Files:
The mode argument in the open() function allows you to specify how you want to interact with the file. Below are some common modes:
Read mode ('r'): Opens the file for reading (default mode). Raises an error if the file doesn’t exist.
file = open("example.txt", "r")
Write mode ('w'): Opens the file for writing, creating a new file if it doesn’t exist. If the file exists, its contents are erased.
file = open("example.txt", "w")
Append mode ('a'): Opens the file for writing, but new data is added to the end of the file without deleting its contents.
file = open("example.txt", "a")
Exclusive creation mode ('x'): Creates a new file and opens it for writing. Raises an error if the file already exists.
file = open("example.txt", "x")
3. Binary vs Text modes:
Python allows files to be opened in either text mode or binary mode, with 't' and 'b' suffixes in the mode argument:
Text Mode ('t'): Opens the file in text mode (default). This mode is used for reading and writing text data. When reading files, newline characters are handled automatically.
file = open("example.txt", "rt") # "r" is the same as "rt"
Binary Mode ('b'): Opens the file in binary mode. This is typically used for non-text files like images or videos, where data is handled as bytes and no character encoding is applied.
file = open("example.jpg", "rb")
Using combinations of these, you can specify modes such as:
- "wb": Open a file for binary writing.
- "rb": Open a file for binary reading.
4. Closing files:
To avoid resource leaks, always close files after completing operations. Use the 'close()' method to release the file:
file.close()
A more efficient way to handle files is with the 'with' statement, which automatically closes the file after the block of code completes:
with open("example.txt", "r") as file: content = file.read() # File is automatically closed after the block
5. Practical examples and exercises:
Exercise 1: Opening and reading files:
1. Open a text file in read mode and print its contents. Close the file afterward.
2. Use the 'with' statement to read a file and display its contents without explicitly calling 'close()'.
Exercise 2: Writing and appending to files
1. Open a file in write mode and write some text. Open it again in read mode to verify its contents.
2. Open the file in append mode and add more text. Read and print the updated content.
Exercise 3: Working with binary files
1. Open an image file in binary read mode, read a portion of it, and print the raw binary data.
2. Write a program that creates a new binary file and writes raw byte data to it.
Conclusion
In this lesson, students learned how to open and close files using the 'open()' function in Python. They explored different file modes, including read, write, append, and binary, and saw examples of how to handle files efficiently. Understanding file modes and proper file handling is essential for managing data input and output in Python applications.