There's this line in 'file.txt'
that says:
memcpy(buffer, "", 64)
I want to write a Python script that reads this file.txt
and locates this line in it. Then, it proceeds to insert a specific string (stored in a variable 'data') between the two quotes ("") in that line.
For example, if the variable data
contains the value 'foo bar'
, then the line would be modified by the Python script to:
memcpy(buffer, "foo bar", 64)
Here's what I have tried:
data = "foo bar"
with open('file.txt') as in_file:
in_buffer = in_file.readlines()
with open('new.txt', "w") as out_file:
for line in in_buffer:
if line == 'memcpy(buffer, "", 64)':
out_file.write("memcpy(buffer, \"data\", 64)")
else:
out_file.write(line)
It didn't work because it created the exact same replica of the file in new.txt without making any modifications.