← Back to Files
Question 433
Replace Across A File
Replace Across A File
Write the three lines in draft to draft.txt, then read two words from input: the word to find and the word to put in its place. Read draft.txt into a list with readlines, and write every line to final.txt with the old word swapped for the new one using the string method replace, counting the lines in which the old word occurred (test with in before replacing). Finally read final.txt with f.read(), print it with end="", then print Replaced in <n> line(s). Input: two lines, no prompt.
Expected Output:
the dog sat on the mat a dog naps Replaced in 2 line(s)
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Files
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.