Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

def main(): found = False search = input (\'Which one?\') student_file = open (\

ID: 3849624 • Letter: D

Question

def main(): found = False search = input ('Which one?') student_file = open ('s.txt', 'F') temp_file = open ('t. txt', 'w') descr = student_file. Readline() while descr l = ' ': name = str (student_file. readline()) descr = descr rstrip(' ') if descr l = search: temp_file. write (descr + ' ') temp_file. write(name + ' ') else: found = True descr = student_file. readline() student_file. close() temp_file. close() os. remove ('s. txt') os. rename ('t. txt', 's. txt') os. remove ('John Perz') if found: print ('The file has been updated!') else: print ('The item in the file has not been found!') main() What happens when the program above is executed? Clearly explain Assume that s txt exists and its format is correct. remove os.remove("John Perz") otherwise output will be shown as" John Perz has been deleted"

Explanation / Answer

Above program asks a string to search.Read data(description and name) from a file (s.txt). Then copy every pair (description and name) in which search string is not found to another file t.txt .

The the program removes the source file (s.txt) . and then rename the destination file (t,txt) to s.txt

Then the program will check if the search string is present in originall s.txt file . if found it show a message that "the file has been updated" . otherwise it show a message that " the item in the file has not been found'

Example:

Let content of s.txt

xxxx

Rakesh

yyy

Bikash

ppp

Kapil

yyy

Harmohan

sss

SSG

---------------------------------

and let user input the search string as "yyy" .

Then, as yyy ispresent is s,txt , it will be modified to

-------------------------------

xxxx

Rakesh

ppp

Kapil

sss

SSG

-----------------------

and a mesage will bve shown that "the file has been updated"