Aug-08-2021, 04:59 PM
You did not create a function and your indentations are disastrous, try this
import os import re folder_path = "/home/pi/Workspace/Case_02/" # your path target = "Format" def str_counter(match_object): str_counter.count += 1 return str(str_counter.count) str_counter.count = 0 def do_it(file_name): with open(file_name, 'r') as file : filedata = file.read() print(filedata) filedata = re.sub(re.escape(target), str_counter, filedata) print(filedata) with open(file_name, 'w') as file: file.write(filedata) file.close() for name in os.listdir(folder_path): if name.endswith(".txt"): file_name = os.path.join(folder_path, name) do_it(file_name)