Question

In: Computer Science

Send this as a Python file. Note: this is an example where you have the original...

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

Solutions

Expert Solution

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-----------------

Related Solutions

Provide an original example of a transaction and then identify the type receivable—accounts receivable, a note...
Provide an original example of a transaction and then identify the type receivable—accounts receivable, a note receivable, or other receivables.
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.
{PYTHON }You have a CSV file containing the location and population of various cities around the...
{PYTHON }You have a CSV file containing the location and population of various cities around the world. For this question you'll be given a list of cities and return the total population across all those cities. Write a function named "total_population" that takes a string then a list as parameters where the string represents the name of a CSV file containing city data in the format "CountryCode,CityName,Region,Population,Latitude,Longitude" and the second parameter is a list where each element is itself a...
{PYTHON }You have a CSV file containing the location and population of various cities around the...
{PYTHON }You have a CSV file containing the location and population of various cities around the world. For this question you'll be given a list of cities and return the total population across all those cities. Write a function named "total_population" that takes a string then a list as parameters where the string represents the name of a CSV file containing city data in the format "CountryCode,CityName,Region,Population,Latitude,Longitude" and the second parameter is a list where each element is itself a...
Can you send the excel file if I provide an email address? This is a question...
Can you send the excel file if I provide an email address? This is a question on managerial decision making with excel spreadsheet. A step wise how to solve this will be appreciated for homework and not any exam. A model in excel will really help. John’s Truck John owns John’s Truck Rental, which is currently for sale for $650,000. He would like you to develop a five-year spreadsheet model to assist buyers in their evaluation of the company. The...
c# Create a console application that protects an XML file, such as the following example. Note...
c# Create a console application that protects an XML file, such as the following example. Note that the customer's credit card number and password are currently stored in clear text. The credit card must be encrypted so that it can be decrypted and used later, and the password must be salted and hashed: <?xml version="1.0" encoding="utf-8" ?> <customers> <customer> <name>Bob Smith</name> <creditcard>1234-5678-9012-3456</creditcard> <password>Pa$$w0rd</password> </customer> </customers>
In Python please. Construct a large file of data in random order where the key is...
In Python please. Construct a large file of data in random order where the key is a 13-digit number. Then sort in two different ways. Use a Quick sort and then use a radix sort of some kind. Code both sorts in the same language and run experiments to see at what size of data does the radix run faster than the Quick sort.
BEFORE YOU START MAKE SURE TO SEND AS A TEXT FILE NOT PDF OR PICTURES. WRITE...
BEFORE YOU START MAKE SURE TO SEND AS A TEXT FILE NOT PDF OR PICTURES. WRITE IN A NEXT FILE IN JAVA ECLIPSE FORMAT. WILL BE POSTING THE CODE ON JAVA ECLIPSE. ALSO WILL GIVE THUMBS UP Write a class called Dog that contains instance data that represents the dog’s name and age. Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to compute and return...
Provide an original example where you perform a hypothesis test using the one-mean z-test. Provide within...
Provide an original example where you perform a hypothesis test using the one-mean z-test. Provide within this part the following items: State the null hypothesis. State the alternative hypothesis test. State the chosen alpha level of significance. Use the critical value approach to determine whether to reject or fail to reject the null hypothesis. Use the p-value approach to determine whether to reject or fail to reject the null hypothesis. Discuss how a Type I or Type II error could...
For example, I have an input text file that reads: "Hello, how are you?" "You look...
For example, I have an input text file that reads: "Hello, how are you?" "You look good today." "Today is a great day." How can I write a function that inputs each line into a node in a linked list? Thank you. (Note: the input text files could have a different amount of lines, and some lines could be blank.) This is the linked list code: #include <iostream> #include <cstdlib> #include <fstream> class Node { public: Node* next; int data;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT