The file read operations in Python are of three types.
- Read file contents as text (Text Mode)
- Read file contents as binary (Binary Mode)
- Read file contents as binary buffering (Raw mode)
1. Reading file contents as text in Python
The built-in function open() by default opens a file in text mode. The contents read on the file object are returned as Python string - a str object.
(a) Example: Read by specifying the byte count:
# Example Python program that reads a file in text mode. filePath = "./alice.txt" # Number of bytes to read # Read the contents in one go |
Output:
Number of bytes to read: |
(b) Example: Read the whole file:
file = open(filePath, "r") |
The above example will read the whole file contents into the variable contents and print to the console. Care must be taken to make sure that there is enough physical memory to support this read operation, which reads any file contents in one go.
(c) Read the file line by line:
file = open(filePath, "r") |
The above example will read line by line. EOF or End of File in Python is an empty character. When the EOF character is found, the program will break and exit.
However, while(True) is not recommended. Remember, writing
while (aLine = PythonFileObject.readline() is not '') |
is not supported in Python.
So what is the better way to read multiple lines one at a time in Python? One can write using for loop like below.
for aLine in file: |
2. Read file contents as binary:
While reading a file in binary mode, the programmer can specify the offset from the beginning of the file to read from.
# Example Python program that opens a file # Open the file to read in binary mode # Move "n" positions # Read one line |
Since the file contents are read as binary, the output is printed like below.
b'.. for it is the beginning of always\xe2\x80\x9d\n' |
3. Read file contents as raw bytes:
In raw file I/O mode the file is read as binary without any buffering which means any optimisation like buffered reading to be done by the programmer herself. Raw I/O in Python is achieved by turning off the buffering with a zero value for the buffering parameter in the open() method.
# Example Python program that reads a file in raw mode filePath = "dilbert.txt" # Open in raw mode - no buffering # Move past "n" bytes # Read the remaining bytes and print them |
Output:
b"what you need, and I'll tell you how to get along without it." |
The output is very much similar to the binary mode. However since buffering is disabled in Raw I/O a noticeable performance difference
could be found while reading a file with lot of data.