![<function getdata at 0x7fbad18a5050> python <function getdata at 0x7fbad18a5050> python](https://i.pinimg.com/originals/0f/0f/5c/0f0f5ca7d591d4064a0247970e720510.jpg)
Step 9: Here we are taking inputs from user after qualifying user for updating records. Here at 11 part we are just writing the same data in ‘temp.txt’ file we are reading from ‘text.txt’ file. 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). Step 8: Here we are checking inside our list ‘L’ at 0 index the value is same as user is searching to update. So ‘s’ is always storing some value and length of s is also greater than 0.īut 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. Length of s should always be greater as we are reading values and storing inside s. We only enter inside if statement if condition is True. Step 7: Here we are checking for the length of s > 0. What split() function do is, it just separate each word by specified character ie ‘ ~‘. Step 6: Here we have taken the variable ‘L’ and stored value of ‘s’ after splitting from ‘~’ sign.
# python code#
This code read each line inside file and keep on storing in variable ‘s’. Step 5: After entering inside while() the first statement execute is fh_main.readline(). If we empty variable then it will not enter inside while loop and directly jump to the statement number 12. 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 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. Important: We passed ‘s’ in while loop and ‘s’ is storing a space which means some value. Step 15: Here we have used while() loop it will keep on executing while condition remains true. It is storing a space and space means a string value. Step 4: Here we have taken a variable s = ‘ ‘. So we will get the input from user and filter student detail by its roll number.
# python update#
Step 3: We are going to search data we want to update by roll number. “temp.txt” which is our temporary text file.
![<function getdata at 0x7fbad18a5050> python <function getdata at 0x7fbad18a5050> python](https://sandippatilprogrammer.files.wordpress.com/2017/12/d1.png)
Step 2: Here we have opened new empty file ie. We have opened “text.txt” file in read mode. Step 1: Here I have created file object (fh_main). We have used OS functions like remove() and rename() which has pre-defined in OS library. We will update record of student having roll number : 20. I will explain you each line of code in detail. Program Explanation: Here I have completed complete program in 14 to 15 lines of code. We will solve our problem by using this functions and also we use some core concepts of Python programming.
![<function getdata at 0x7fbad18a5050> python <function getdata at 0x7fbad18a5050> python](https://www.nurmatova.com/uploads/3/7/4/1/37416577/screen-shot-2020-11-07-at-3-20-11-am_orig.png)
# python how to#
Problem Solving Approach: Till now we have discussed, how to read, write and update data from a file. We will write a code to update any one record from our file and rest of our data to remain un-change. We have stored roll no., name, class, and section separated by “ ~” sign.
![<function getdata at 0x7fbad18a5050> python <function getdata at 0x7fbad18a5050> python](https://low-orbit.net/images/return-multiple-values-python-class.png)
In above given image we have a file having entries of four students. You can see below image to see the data written to the file. So, let’s suppose I have a file named “test.txt”. Update data from file: To update any data from a file, first of all its necessary for us to have a file with some data written to it. We use our own logic to update data from our text file. There is no any pre defined function in python to update data from a file. Python provides us the read, write, delete and append functions in file handling. Update Data From File in Python Programming