Question

Restrictions • You're not allowed to make more than one pass reading a file • You’re...

Restrictions • You're not allowed to make more than one pass reading a file • You’re not allowed to modify the input file in any way • The only functions you're allowed to use are: open, close, read, write, readline, range, len, append, next, split, strip, join

2. Text replacement Write a function replace_text that takes three parameters, a file name and two strings (not words!), and creates a new file named output.txt that is a copy of the input file where every occurrence of the second parameter has been replaced by the third parameter.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please comment below for any queries...
Please don't forget to upvote if the answer is helpful thanks..


Executable Code:

def replace_text(inFile, firstString, secondString):
    lines = open(inFile).read()
    writer = open('output.txt', 'w')
    l=secondString.join(lines.split(firstString))
    writer.write(l)
    writer.close()
#Sample Output:

replace_text('input.txt', 'Baa, Baa, Black Sheep' , 'Bee, Bee, White Sheep')

input.txt


output.txt after executing :

Add a comment
Know the answer?
Add Answer to:
Restrictions • You're not allowed to make more than one pass reading a file • You’re...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT