Replace string within file contents

后端 未结 8 1754
不知归路
不知归路 2020-12-01 00:28

How can I open a file, Stud.txt, and then replace any occurences of \"A\" with \"Orange\"?

8条回答
  •  借酒劲吻你
    2020-12-01 01:07

    with open('Stud.txt','r') as f:
        newlines = []
        for line in f.readlines():
            newlines.append(line.replace('A', 'Orange'))
    with open('Stud.txt', 'w') as f:
        for line in newlines:
            f.write(line)
    

提交回复
热议问题