Delete Data From File in Python Programming

Python provides us the readwritedelete and append functions in file handling. There is no any pre defined function in python to update data from a file. We use our own logic to update data from our text file.

python-video-tutorials

Delete data from file : To delete any data from a file, first of all its necessary for us to have a file with some data written to it. So, let’s suppose I have a file named “test.txt”. You can see below image to see the data written to the file.

file: test.txt
delete-data-from-file
In above given image we have a file having entries of four students. We have stored roll no.nameclass, and section separated by “~” sign.

We will write a code to delete any one record from our file and rest of our data to remain un-change.

Problem Solving Approach: Till now we have discussed, how to read, write and append data in a file. We will solve our problem by using these functions and also we use some core concepts of Python programming.

Solutions:

delete-data-from-file-in-python

delete-data-in-file-handling-python

Program Explanation: Here I have completed complete program in 11 to 12 lines of code. I will explain you each line of code in detail. We will delete record of student having roll number : 20.

Step 0: Here I have imported OS module. We have used OS functions like remove() and rename() which has pre-defined in OS library. Refer line 11 and 12.

Step 1: Here I have created file object (fh_main).  We have opened “text.txt” file in read mode.

Step 2: Here we have opened new empty file ie. “temp.txt” which is our temporary text file. We will use this file to write our data.

Step 3: We are going to search data we want to delete by roll number. So we will get the input from user and filter student detail by its roll number.

Step 4: Here we have taken a variable s = ‘ ‘. Note:  Here ‘s’ is not an empty variable. It is storing a space and space means a string value.

Step A: Here we have used while() loop it will keep on executing while condition remains true. Important: We passed ‘s’ in while loop and ‘s’ is storing a space which means some value. If we use a variable storing some value inside while loop it acts as True and if ‘s’ does not storing value than it acts as False.
We want our program to enter inside while loop that’s why we defined ‘s’ in such a way that it acts as True. If we empty variable then it will not enter inside while loop and directly exit from while loop.

Step 5: After entering inside while() the first statement execute is fh_main.readline(). This code read each line inside file and keep on storing in variable ‘s’.

Step 6: Here we have taken the variable ‘L’ and stored value of ‘s’ after splitting from ‘~’ sign. What split() function do is, it just separate each word by specified character ie ‘~‘. It stores each value in list form ie. [].

Step 7: Here we are checking for the length of s > 0. We only enter inside if statement if condition is True. Length of s should always be greater as we are reading values and storing inside s. So ‘s’ is always storing some value and length of s is also greater than 0.

But when we reach end of line then s does not storing any value as there is no more data and that time length of should become 0 and next execution process should stop automatically.

Step 8: Here we are checking inside our list ‘L’ at 0 index the value is not equal to the roll number or value entered by user. If the roll number entered by user is lying at 0 index of L in any of entry then that record will be skipped and remaining records (except the data user want to delete) will be written in ‘temp.txt’ file.

Step 9: Here we are writing data to our ‘temp.txt’ file.

Note: Here step 8 and step 9 is very important please read once again try understand carefully. We are actually not doing anything with our ‘text.txt’ file. we are writing data in our ‘temp.txt’ file in all cases. User can delete one record at once.

In fact we are created a new file which stores all values except the values entered by user. So here our purpose for deletion of records is done.

Step 10: If we perform any changes in our file and want to make it save or commit. It is necessary to close that files. That’s why to commit all changes we closed our all files here.

Step 11: Now we have two files ‘temp.txt’ which is the new one storing updated records after deleting the record that user want and ‘text.txt’ storing the old non-deleted records. So, now we do not need our old file as this is not of any use. Hence using os.remove() function we deleted our file ‘text.txt’.

Step 12: Here after deleting old ‘text.txt’ file we renamed our new file ‘temp.txt’ as ‘text.txt’ file. Hence we got our updated new ‘text.txt’ file.

Note: It’s OK ! If you are complete beginner in programming than may be you found some difficulties. Else if you have some taste before than it’s not so challenging.