CJCoding With Joseph
15free / daySign inCreate account

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
Topics:
Files
📥 Auto-Input:
cat\ndog\n
This input is automatically fed to your program's stdin
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.