Question

In: Computer Science

Using Python. A file exists on the disk named students.txt. The file contains several records, and...

Using Python.

A file exists on the disk named students.txt. The file contains several records, and each record contains two fields: (1) the student’s name, and (2) the student’s
score for the final exam. Write code that deletes the record containing “John Perz”as the student name.

This the code i have so far but it's not working:

import os

def main():

found=False

search='John Perz'

student_file = open('student.txt','r')

temp_file = open('temp_students.txt','w')

name=student_file.readline()

score=''

while name !='':

score=student_file.readline()

name=name.rstrip('/n')

score=score.rstrip('/n')

if name !=search:

temp_file.write(name+'/n')

temp_file.write(score+'/n')

else:

found=True

name=student_file.readline()

student_file.close()

temp_file.close()

os.rename('temp.text','student.text')

if found:

print("The record was deleted")

else:

print("Record not found")

main()

Solutions

Expert Solution

Short Summary:

Implemented the program as per requirement
Attached source code and sample output


**************Please do upvote to appreciate our time. Thank you!******************

Source Code:

def main():
with open("C:\\Users\\asus\\student.txt") as f:
lines = f.readlines()
for ind, line in enumerate(lines):
if line.startswith("John Perz"):
lines[ind] = ""
with open("C:\\Users\\asus\\student.txt","w") as f:
f.writelines(lines)
main()

Code Screenshot:

Output:

Input file:

OutputFile:


**************Please do upvote to appreciate our time. Thank you!******************


Related Solutions

In Python: Assume a file containing a series of integers is named numbers.txt and exists on...
In Python: Assume a file containing a series of integers is named numbers.txt and exists on the computer's Disk. Write a program that reads al the numbers stored in the file and calculates their total. - create your own text file called numbers.txt and put in a set of 20 numbers (each number should be between 1 and 100). - Each number should be on its own line. - do not assume that the file will always have 20 numbers...
Suppose that we are using extendable hashing on a file that contains records with the following...
Suppose that we are using extendable hashing on a file that contains records with the following search-key values: (2, 3, 5, 7, 11, 17, 19, 23, 29, 31). Show the final extendable hash structure for this file if the hash function is h(x) = x mod 8 and buckets can hold three records. Recall that: 1. the bucket address table is indexed by the prefix of the binary representation of h(x). 2. Initially i = 0, i.e. the prefix consists...
Assume a file containing a series of words is named words.txt and exists on the computer’s...
Assume a file containing a series of words is named words.txt and exists on the computer’s disk. The program should generate the set of unique words in the document and display the number of times each word in the list appears in the document.
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file must satisfy the following. Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Consider a text file that you will create named “employees.txt”. The file contains data organized according...
Consider a text file that you will create named “employees.txt”. The file contains data organized according to the following format:John Smith 10 15Sarah Johnson 40 12Mary Taylor 27 13Jim Stewart 25 8For instance, “John” is the first name, “Smith” is the last name, “10” is the number of hours per week, and “15” is the hourly rate.Write a program that computes the weekly salary of each employee. The program prints the first name, last name, and weekly salary of each...
Python: Trivia Questionaire Create a trivia application using the provided file which contains 15 questions in...
Python: Trivia Questionaire Create a trivia application using the provided file which contains 15 questions in it. Your application should randomly select how many questions will be presented to the user and it must be a number not greater than the number of questions in the file. Every question that is displayed should be randomly selected from the list and should be non-repetitive; in other words, you should not ask the same question twice. Your application should keep track of...
Python, filing and modules Modify the driver to write the statistics to a file named employee-stats.txt...
Python, filing and modules Modify the driver to write the statistics to a file named employee-stats.txt rather than to the console. driver.py code: from employee import Employee employees = [] # Load an employee object for each employee specified in 'employees.txt' into the employees list. employee.txt Jescie Craig VP 95947 Marny Mckenzie Staff 52429 Justin Fuller Manager 72895 Herman Love VP 108204 Hasad Wilkins Staff 96153 Aubrey Eaton Staff 76785 Yvonne Wilkinson Staff 35823 Dane Carlson Manager 91524 Emma Tate...
do not use these: {,},{} Python 3 APPL 276.75 15 A file named asset.txt consists of...
do not use these: {,},{} Python 3 APPL 276.75 15 A file named asset.txt consists of only the three lines above. They describe the unit cost and quantity of shares of Apple stock. Write statement that open the file, read the data and use it to print this exact output: My APPL stock is worth $4,151.25.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT