Hi I'm trying to write a python script that will...
find and replace certain values in a file. The problem being there are multiple files called the same name and not all of them will have the values.
It would also be good to take backup of the file before changing the values.
So far I have managed to get as far as searching for the file with the name test1.txt. the out put shows multiple locations of where it has found test1.txt next I need it to search for the values such as "Scott" and replace it with " John" and take a backup before changing it if possible.
Not all the test1.txt found will have the name "Scott" so I don't really want to take a backup of them all.
import os
filename[]
def find_files(filename, search_path):
result = []
for root, dirs, files in os.walk(search_path):
if filename in files:
result.append(os.path.join(root, filename))
return result
print(find_files("test1.txt", '/home/')