File handling in Python [ All Text File, Binary File operations with Source code ]

File handling in Python is basically divided into two parts- Text File handling and binary file handling. Both types of files need some basic operations like
1. Opening a file
2. Closing a file
And in between, we need some sort of processing on these two types of files. This processing may be reading file contents or writing new content or deleting /updating/searching.

Text binary file handling in Python
In the next couple of file handing articles we will discuss different aspects of file handling and how do we manage them.
Let’s discuss how we can open a text file in python. Python has an open() function to open any text/binary file. The syntax of open() function in python is as follows

filehandler = open(“filename”,mode)

So, if we want to open a text file “abcd.txt” in python then the command is as follows

file1 = open(“abcd.txt”)

By default open() function open text file in reading mode i.e. you can only read the contents of this file but cannot write/update anything in this file.

File Modes available in Python

File mode defines the behavior of a file. These modes can be used as a single entity or with a combination of others. The following list contains all these modes along with their basic definition

“r”      — for reading. This is default when a file is opened using open() function

“w”     — for writing

“a”      — for appending ie it will preserve the old contents and write new contents in the same file

“r+”     — for reading and writing both at the same time in the file

“b”      — binary mode – If used then the file is ready for binary manipulation

The close () function is used to close an opened file. The syntax of close() function in python is as follows
Filehandler.close()

Thus our command to close previously opened file should be like this

File1.close()

Reading Data from Text File

Unlike other programming languages like C/C++ python read the whole file contents in a single shot and store the same in the cache using read() function. This cached data we can be stored in a python variable to manipulate it.

Python program to read a text file

file = open("abcd.txt")
data1 = file.read()
print(data1)
file.close()

The above program read the content of the abcd.txt file and display them on the screen.

Point to remember: This file abcd.txt must be in your directory otherwise above program will show “file not found error”. If want to open any file from another location, provide the complete absolute path of the file.

Besides read() function, python also provides readline() function that reads one line at a time from the file.
So the next python program will count the total number of lines in my text file abcd.txt.

# program to find out the total number of words in any given test file
# made by        : rakesh kumar
# made on       : 07/07/2018

file = open(r"abcd.txt")
words = lines = 0
for line in file.readlines():
    lines += 1
    words += len(line.split())
file.close()
print("Total Words in this File  :", words)
print("Total Lines in this File  :", lines)

Now we are going to write a python program to find out all the lines that start with alphabet ‘T’ in a text file in “abcd.txt”.

The idea behind this program will be – read one line at a time using readline() function and store that line python string. Compare the first character of this string and display an appropriate message.

#program to find out total number of lines that start with alphabet ‘T’
#made by        : rakesh kumar
# made on       : 07/07/2018   

file =  open(r"abcd.txt",'r')
count=0;
for line in file.readlines():
    if line[0]=='T':
        count+=1
file.close()
print("Total lines that start with alphabet 'T'  :",count)

Another very interesting program is to find out a word in a text file “abcd.txt”.
The idea behind this program in – read the content of a file in a python variable. Split the contents into words using split() method. Now compare our word with the words. Here is the complete python code.

#   python program to search a word in a text File
#   program by          : rakesh kumar

word = input('Enter any word that you want to find in text File :')
word1= '"'+word+'"'
word2= "'"+word+"'"
f = open("rakesh.txt","r")
data = f.read().split()
if word in data or word1 in data or word2 in data:
    print('Word Found in Text File')
else:
    print('Word not found in Text File')

Now the last program that I would suggest in text file handling is – Remove all extra white spaces from out given text file “abcd.txt”
Extra spaces mean – multiple spaces or multiple blank lines available in our file. The program should remove all the extra spaces as well as tabs and newlines.
Python Program to remove spaces/tabs and newlines

import re
file = open("abcd.txt")
data = file.read()
data = re.sub('[ \t\n]+', ' ', data)
print(data)

The above program used regular expressions to find out extra spaces and replaced them with single space.
These text file reading programs are sufficient to give you a basic understanding of text file reading.

Creating / Writing Text file in Python

Text file creation/writing in a text file in python is equally easy. The process is almost the same as reading a text file ie you have to open a file, close a file and in between, you are either putting new text or editing existing text.
Syntax to open a text file in Writing mode

Filehandler  = open(“filename”,”w”)

Now file mode is compulsory to write anything in our text file. It was optional for reading a text file. The open() function open this file in writing mode erasing previous contents if this file already exists in your current folder if this does not exists then generate a new file.

File1  = open(“cbsetoday.txt”,”w”)

Now, we can write anything in this file using our own filehandler.

file.write("This is rakesh and this is my first file handling program in Python \n")
file.writelines("third line of the same program")

Write() and writelines() are two functions to put data in files. The only difference is writelines() function automatically add a new line at the end of data.
In the same way, we can close this file. The syntax for closing files is exactly the same as the reading a file.

File1.close()

So the overall program to write contents in a text file is like this

#program to create Text File using Python
#   made by             : rakesh kumar
#   Last Compiled on    : 7/7/2018

file = open("Testing.txt","w")
file.write("This is rakesh and this is my first file handling program in Python \n")
file.write("This is second line and i am learning new things in pythin")
file.writelines("third line of the same program")
file.close()
print("File Generated please check")

The only disadvantage of the above approach is – it removes previous contents of an existing text file.

In order to preserve the previous contents- We need one more type of file mode ie “a”. This mode stands for “append” ie it preserves the previous contents and allows you to add your content at the end of existing data.
The modified program to write data in a text file is as follows

#program to create Text File using Python
#   made by             : rakesh kumar
#   Last Compiled on    : 27/11/2019

file = open("Testing.txt","a")
file.write("This is rakesh and this is my first file handling program in Python \n")
file.write("This is second line and i am learning new things in pythin")
file.writelines("third line of the same program")
file.close()
print("File updated please check")

These are two basic operations that all python programmers must know to manage all types of text files using python.

abcd.txt file for download for texting above program

Binary file handling in Python [ read / write / delete/ update – Source Code ]

Binary files are the files that are stored in the memory in binary mode ie you can not read these types of files using any text editor like notepad or sublime or visual studio code. Read more on binary file at Wikipedia

Advantages of Binary files

  • Binary files are faster than text file in processing
  • The content of binary files can not be copied using text editors
  • They take less space compared to text files

The creation of binary files comes first compared to text files. We can generate Text files using any text editor but the same is not true for binary files.

Binary files need a special mechanism to generate but opening and closing of the binary file remain the same as text files thus we are not going to discuss the opening and closing methods of binary files.

Pickle is a special python package that is used to generate data in binary format. Pickle comes with few methods like dump() to write data in binary format.

Python Program to Create a binary file

#   program to create a binary file of records - Just like structure
#   made by         : rakesh kumar
#   Compiled on     : 27-Nov-2019

import pickle
list =[]
while True:
    roll = input("Enter student Roll No:")
    sname  = input("Enter student Name :")
    student = {"roll":roll,"name":sname}
    list.append(student)
    choice= input("Want to add more record(y/n) :")
    if(choice=='n'):
        break

file = open("student.dat","wb")
pickle.dump(list,file)
file.close()

Point to remember: While opening a file in binary mode, Please note that it must contain “wb” where ‘b’ stand for binary.
The above program generates a user-generated dictionary of students and then using the pickle dump() method to write the whole list in student.dat file. The output of the above program looks like this

Reading Binary file using Python Pickle

Reading a binary file is also as easy as reading a text file. All you need is the pickle.load( ) method. The program to read a binary file is as follows

# program to read a binary file of records - Just like structure
#   made by         : rakesh kumar
#   Compiled on     : 8-July-2018

import pickle

file = open("student.dat", "rb")
list = pickle.load(file)
print(list)
file.close()

Point to Remember: While opening a binary file for reading “rb” is compulsory as it will direct python to open your file in binary mode.

The output of the above program is as follows

Output of binary file using python

Searching a Record in a binary file

Searching in binary file involves reading of the binary file and then comparing each record with our given value, Thus we are bound to use the linear search method.

Sample Python program to search data in the binary file using the linear search algorithm is as follows

# program to search a name in binary file
#   made by         : rakesh kumar
#   Compiled on     : 8-July-2018

import pickle

name = input('Enter name that you want to search in binary file :')

file = open("student.dat", "rb")
list = pickle.load(file)
file.close()

found = 0
for x in list:
    if name in x['name']:
        found = 1
print("Found in binary file" if found == 1 else "Not found")

 

Update a Record from a Binary file

The idea behind this program is – we will open our binary file in reading and write mode using “rb+”. Read the whole data in a list. Now get the value that we need to update and check this value in this list using linear search method.

If the record is available, Move the file pointer at the beginning of the file using seek() method and rewrite the whole contents on the same file.

Python program to update a binary file using pickle is as follows

# program to update a name in binary file
#   made by         : rakesh kumar
#   Compiled on     : 8-July-2018

import pickle

name = input('Enter name that you want to update in binary file :')

file = open("student.dat", "rb+")
list = pickle.load(file)

found = 0
lst = []
for x in list:
    if name in x['name']:
        found = 1
        x['name'] = input('Enter new name ')
    lst.append(x)

if(found == 1):
    file.seek(0)
    pickle.dump(lst, file)
    print("Record Updated")
else:
    print('Name does not exist')

file.close()

Delete a record from binary file

The deletion of records from a binary file is also based on modifying a record. We are again going to open our file in read-write mode using “rb+” and search the given name in this list. If it is available on our list then do not add this data in our new file.

Move file pointer at the beginning of the file using file.seek() method and dump whole new list our existing binary file. Thus overwriting the existing data.

Python program to delete a record from a binary file is as follows

# program to update a name in binary file
#   made by         : rakesh kumar
#   Compiled on     : 8-July-2018

import pickle

name = input('Enter name that you want to DELETE from binary file :')

file = open("student.dat", "rb+")
list = pickle.load(file)

found = 0
lst = []
for x in list:
    if name not in x['name']:
        lst.append(x)
    else:
        found = 1

if(found == 1):
    file.seek(0)
    pickle.dump(lst, file)
    print("Record Deleted ")
else:
    print('Name does not exist')

file.close()

I hope these concepts will help you to understand file handling in Python. Though there are other file methods that can be used to manipulate binary files using python, I think these are the easiest methods available.

We missed few concepts like file modes and binary file methods to read few words or few bytes from this tutorial but I think you people are already aware of them, If you want to learn more about them. Please let us know via our email ID.

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.