In: Computer Science
Send this as a Python file. Note: this is an example where you have the original file, and are writing to a temp file with the new information. Then, you remove the original file and rename the temp file to the original file name. Don't forget the import os statement.A file exist on the disk named students.txt The file contains several records and each record contains 2 fields :1. The student's name and 2: the student's score for final exam. Write a code that changes Julie Milan's score to 100.
Here is text
Mark Lozoya
85
Hector Guevara
98
Jeff Gohrick
79
Julio Gonzalez
90
Dwayne Hudson
81
Julie Milan
92
Johnny Lechuga
90
Mark Martinez
91
Mary Robinson
100
Norma Batista
69
Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code.Below is the output of the program: students.txt before the code is executed:
Console output:
students.txt after the code finishes execution:
Below is the code to copy: #CODE STARTS HERE----------------
import os
#importing name and score for updation
name = input("Enter the name of student:")
score = input("Enter the score: ")
f_temp = open("temp.txt", 'a')#creating and opening a temp file
with open("students.txt",'r') as f:#open students.txt
   key = 0 #Key becomes 1 if student name is found
   for line in f: #loop line by line
      if key == 1: #if name found, make changes to score
         f_temp.write(score+"\n") #write updated score
         key=2 #change key status once changes are made
         print("Score updated")
         continue
      f_temp.write(line) #Just update from students to temp
      if name.lower() in line.lower(): #Check if name is found
         key = 1 #change key value
   if key == 0: #key will be 2 if any match happens
      print("Name not found")
f_temp.close() #close temp file
if os.path.exists("students.txt"): #check and remove students.txt
   os.remove("students.txt")
else:
   print("File delete Error")
if os.path.exists("temp.txt"): #Check and rename temp.txt
   os.rename("temp.txt", "students.txt")
else:
   print("File rename Error")
#CODE ENDS HERE-----------------