19
Lesson 19
Random access to files
Objective
By the end of this lesson, students will understand how to perform random access operations on files in Python using the seek() and tell() methods. They will learn how to manipulate the file pointer to read and write data at specific locations within a file.
1. Introduction to random access:
Random access allows reading from and writing to any location in a file, rather than sequentially. This capability is essential for efficiently handling large files or for applications requiring frequent updates to specific data points.
2. The file pointer:
By the end of this lesson, students will understand how to perform random access operations on files in Python using the seek() and tell() methods. They will learn how to manipulate the file pointer to read and write data at specific locations within a file.
1. Introduction to random access:
Random access allows reading from and writing to any location in a file, rather than sequentially. This capability is essential for efficiently handling large files or for applications requiring frequent updates to specific data points.
2. The file pointer:
- The file pointer is an internal marker that indicates the current position within a file.
- When a file is opened, the pointer is positioned at the beginning by default.
- Understanding how to manipulate the file pointer is crucial for random access operations.
3. The seek() method:
The seek(offset, whence) method is used to move the file pointer to a specified location.
Parameters:
offset: The number of bytes to move the pointer.
whence: The reference point for the offset (default is
- 0 : Beginning of the file
- 1 : Current file position
- 2 : End of the file
with open("example.txt", "r+") as file: file.seek(10) # Move to the 10th byte data = file.read(5) # Read the next 5 bytes print(data)
4. The tell() method:
The tell() method returns the current position of the file pointer in the file.
This is useful for tracking where you are in the file during read and write operations.
with open("example.txt", "r") as file: print(file.tell()) # Output: 0 (at the beginning) file.read(10) print(file.tell()) # Output: 10 (after reading 10 bytes)
5. Random access in Binary files:
Random access can also be performed on binary files. The methods seek() and tell() work similarly for both text and binary files.
It’s essential to use the correct mode ('rb', 'wb', 'r+b', etc.) when working with binary files.
with open("image.jpg", "rb") as file: file.seek(100) # Move to the 100th byte data = file.read(10) # Read 10 bytes
6. Writing and reading with random access:
Random access is especially useful when you need to update specific records in a file without rewriting the entire file.
with open("data.txt", "r+") as file: file.seek(20) # Move to the position where the data needs to be updated file.write("Updated data") # Write new data at that position
7. Use Cases for random access:
- Database files: Modifying records without affecting the entire file.
- Log files: Adding entries in specific positions or reading from particular locations.
- Configuration files: Quick access and modification of settings.
8. Best practices for random access:
- Always ensure that the file is opened in a mode that supports both reading and writing when using seek() and tell().
- Be cautious of the current position of the file pointer before performing read or write operations.
- Consider the implications of file size and data structure when performing random access to avoid data corruption.
9. Exercises:
Exercise 1: File pointer movement
1. Write a program that opens a file, reads a specific number of bytes from a specific position using seek(), and prints the content.
Exercise 2: File pointer tracking
1. Create a function that opens a file, moves the file pointer around using seek(), and uses tell() to log each position before and after reading data.
Exercise 3: Updating data
1. Implement a script that opens a file, seeks to a specified location, and updates that section with new data. Verify by reading the updated section.
Exercise 4: Random access in Binary files
1. Write a program that opens a binary file, seeks to a certain byte, and reads a fixed number of bytes. Display the result in a readable format.
Conclusion
In this lesson, students learned about random access to files in Python using the seek() and tell() methods. They explored how to manipulate the file pointer to read and write data at specific locations, which is crucial for efficient file handling in various applications. Understanding these concepts will enable them to develop more sophisticated file processing functionalities in their programs.