File I/O in Python - Read operations

File read operations in python are of three types.

  1. Read file contents as text (Text Mode)
  2. Read file contents as binary (Binary Mode)
  3. Read file contents as binary buffering (Raw mode)

1. Reading file contents as text in Python

(a) Example: Read by specifying the byte count: 

 

PythonFileObject = open("ComputingPioneers.txt", "r")
ByteCount        = 43
ReadContents     = PythonFileObject.read(ByteCount)
print("File Contents:")
print(ReadContents)

 

Here are the contents of the input file file Pioneers.txt, listing some of the Pioneers in Computing:

Input File ComputingPioneers.txt

Charles Babbage
C.A.R. Hoare
Dennis Ritchie
Edsger Dijkstra
George Boole
John Backus
John von Neumann
John W. Tukey
Kristen Nygaard
Niklaus Wirth

 

The above example will open the file Pioneers.txt in text mode, and read 43 bytes of text. The program will print the 43 bytes of text it read which is till 'Dennis Ritchie'.

 

(b) Example: Read the whole file: 

 

PythonFileObject = open("Pioneers.txt", "r")
ReadContents     = PythonFileObject.read()
print("File Contents:")
print(ReadContents)

 

 

The above example will read the whole file contents into the variable ReadContents 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: 

 

PythonFileObject = open("Pioneers.txt", "r")
aLine = ''
while(True):
    aLine = PythonFileObject.readline()
    if aLine is not '':
        print(aLine)
    else:
        break

 

 

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 PythonFileObject:
    print(aLine)

 

 

2. Read file contents as binary:

Reading in binary mode the programmer can specify the offset relative from the 
beginning of the file telling exactly from which file pointer position to read.

The example below reads the file Singer.txt with contents
 

 

Aretha Franklin 
Edith Piaf
Elton John
Elvis Aaron Presley
Freddie Mercury
Michael Joseph Jackson

 

 

Remember the file has end of line characters as well.

 

FileObject_BinaryMode = open("Singers.txt", "rb")
FileObject_BinaryMode.seek(50)
Singer = FileObject_BinaryMode.readline()
print(Singer)

 

Since we have read as binary the output is printed like below.

 

b' Presley\n'

 


 

3. Read file contents as raw bytes:


In raw file I/O mode the file is read as binary without any buffering.
That means any optimisation like reading buffered reading to be done by the programmer herself. Raw I/O in python is achieved by turning of the buffering with a zero value for the buffering parameter, in the open() method.

The code below will read the names of the Astronauts who were the crew of Apollo 11 from the file Astronauts.txt,

 

FileObject_RawMode = open("Astronauts.txt", "rb", buffering=0)
FileObject_RawMode.seek(7)
Astronaut = FileObject_RawMode.read()
print(Astronaut)

 

 

 

 

Input file Astronauts.txt:
Neil A. Armstrong
Michael Collins
Edwin "Buzz" E. Aldrin, Jr.

 

 

 

Output:
b' Armstrong\nMichael Collins\nEdwin "Buzz" E. Aldrin, Jr.\n'

 

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.


Copyright 2024 © pythontic.com