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?
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.
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