![]() ![]() Records entered by user is here should be written inside âtemp.txtâ file. 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 codeThis 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 updateStep 3: We are going to search data we want to update by roll number. âtemp.txtâ which is our temporary text file. ![]() 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. # python how toProblem 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. 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 ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |