Reading a File Character by Character
Sometimes you need to process a file at a very granular level, such as reading one character at a time. This is particularly useful when the file’s size is unknown or when you require fine control over data handling. The following example uses theread method with an argument of one to iterate over each character until the end of the file is reached.
Reading a File Line by Line with readline
For many applications, it is more efficient to read a file one line at a time. Thereadline method returns a single line from the file as a string, making it ideal for processing files where each line represents a distinct record. In the example below, we read four consecutive lines from the file:
Reading All Lines with readlines
If you want to work with the entire file as a collection of lines, thereadlines method is a perfect choice. This method reads the complete file and returns a list, where each element is a line from the text file. You can then easily iterate over this list. Below is an example demonstrating this approach:
If you’re processing very large files, consider using an iterative approach (like
readline or reading chunks) to avoid memory issues.Working with Amorphous Data: Bytearrays
In scenarios where you need to handle data without a predefined structure, such as binary files, Python’sbytearray comes in handy. A bytearray is a mutable sequence of bytes, allowing values from 0 to 255. The following code creates a bytearray of size 10 (initialized with zeros) and writes it to a binary file:
Reading from a Binary File with readinto
When you need to read binary data into an existing bytearray, thereadinto method is very efficient. This method reads a fixed number of bytes (matching the size of your bytearray) directly into the array. In the example below, we open a binary file, read its content into a bytearray, and then print each byte in hexadecimal format:
Reading the Entire Binary File with read
Alternatively, you can read the whole binary file into memory using theread method, which is suitable for files that are not excessively large. This approach reads the entire file and converts it into a bytearray. The following code demonstrates this method and prints each byte in hexadecimal:
In this lesson, we’ve covered multiple file handling methods in Python. Whether you’re reading a file character by character, line by line, or in bulk as a binary file, each approach offers unique benefits depending on your application’s requirements.