Writing Multiple Data to File in Python

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

python-video-tutorials

Writing multiple data: Let’s suppose I want to enter records of students to a file “test.txt” ordered by roll no, name, class & section. You can see below image as an example, that what I actually wanted to say.

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

We will write a code to create same file that can store multiple records of students or each student records in each line.

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 this functions and also we use some core concepts of Python programming.

Solution: 
update-data-from-file-python-program

Program Explanation: Here I have completed complete program in 14 to 15 lines of code. I will explain you each line of code in detail. We will update 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 13 and 14.

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 update 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 15: 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 jump to the statement number 12. 

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 same as user is searching to update. If the roll number entered by user is lying at 0 index of L in any of entry then it will move to next operation otherwise execution will move to else part (11). Here at 11 part we are just writing the same data in ‘temp.txt’ file we are reading from ‘text.txt’ file.

Step 9: Here we are taking inputs from user after qualifying user for updating records. Records entered by user is here should be written inside ‘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 update one record at once.

so that one record is written inside the temp.txt file and rest of the records will not enter inside the if loop and directly move to else condition and inside else condition we are also doing the same as we are just writing that details in our ‘temp.txt’ file.

In fact we are created a new file which stores the updated values as well as other data which is available in ‘text.txt’ file. So here our purpose for updation of records is done. 

Step 10: Here after taking updated values from user we written that updated value inside our ‘temp.txt’ file.

Step 11: Just look at the step 8. If the enter roll number entered by user which is not existing inside our file then instead of entering inside if condition it directly jumps to else part ie. step 11 and here we are writing the same records in our ‘temp.txt’ file.

Step 12: 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 13: Now we have two files ‘temp.txt’ which is the new one storing updated records and ‘text.txt’ storing the old un-updated 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 14: 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.