0

How do I create a script to replace a line in a other script

Say I have a file named test 1 and in test one I have Word1 Word2 Word3 And I want to create a script that will replace Word1 with let's say FUBAR Is there any way to do this?

13th Aug 2019, 6:25 AM
Raymond Piacentino Jr.
Raymond Piacentino Jr. - avatar
2 ответов
+ 1
i just wanted to point out some inefficiencies on Jay Matthews 's codes where f.close() is automatic when using with open()... and it is a good practice to always state the mode of the open() function when using them. lastly, looping through the list to write is unnecessary. here is a more efficient version with open("test1.txt", "r") as f: data = f.readlines() data[0] = "FUBAR\n" with open("test1.txt", "w") as f: f.write("".join(data)) note that the newline after FUBAR is necessary to make the word 2 under the FUBAR.
13th Aug 2019, 9:21 AM
Shen Bapiro
Shen Bapiro - avatar
0
in Python you can look up the file() bit of the course :) but I don’t really know, just something I thought I read somewhere
13th Aug 2019, 7:50 AM
Brave Tea
Brave Tea - avatar