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...
Create a Python file named num_sum.py that contains: The definition of two functions: volume - accepts...
Create a Python file named num_sum.py that contains: The definition of two functions: volume - accepts three parameters and returns the product of them, but displays nothing sum_of_nums - accepts 1 or more numbers and returns their sum, but displays nothing A print function call that includes a call to volume, passing 2, 3, and 4 A print function call that includes a call to sum_of_nums, passing 1, 2, 3, 4, and 5
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.
Using the 3 program files below to read the sample students.txt file and create a singly...
Using the 3 program files below to read the sample students.txt file and create a singly linked-list of students. The code in sll_list.h and mainDriver.c shouldn't be changed, only sll_list.c. The finished program should print out a list that initializes the sampled 5 students and allows the user to use the functions found in mainDriver.c. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX // mainDriver.c // #include "sll_list.h" int main() {    int choice,list_size,id;    do    {        printf("1. initialize list of students\n2. add additional student...
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...
Please answer the following using bash shell. 1. The students.txt file consists of rows of students...
Please answer the following using bash shell. 1. The students.txt file consists of rows of students where each row contains a student’s first name, last name, major, and 5 test scores. Write a script that uses a while read statement to input all of the students, line by line, computes the average test score and determines the letter grade for that student. Of the 5 test scores, the 5th test is worth double so that you add each test score...
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file...
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file contains a chronological list of the World Series' winning teams from 1903 through 2018. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2018. (Note the World Series was not played in 1904 and 1994. There are entries in the file indicating this.) Write...
Please use Python! def getText(file): This function should open the file named file, and return the...
Please use Python! def getText(file): This function should open the file named file, and return the contents of the file formatted as a single string. During processing, you should (i) remove any blank lines, (ii) remove any lines consisting entirely of CAPITALIZED WORDS , and (iii) replace any explicit ’\n’ (newline) characters with spaces unless directly preceeded by a ’-’ (hyphen), in which case you should simply remove both the hyphen and the newline, restoring the original word. def getText(file):...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT