Data from coding is not saved

In the experiment, a person has to press the space bar when he sees a certain picture. When I try to save the results, nothing works, or only the last name of the picture is saved. also, the time in which one picture passes and the time of pressing the space bar are not saved. it seems to me that the problem is in the loop, because it saves the last value, but I have already tried copy and deepcopy.

while True:
path = r"D:\psychopy\code\correct\code1"
random_filename = random.choice([
os.path.join(path, x) for x in os.listdir(path)
if os.path.isfile(os.path.join(path, x))
])
img1 = visual.ImageStim(win, random_filename)
img1.autoDraw = True
img1.autoLog = False
win.flip()
thisExp.addData(“random_filename”, copy.deepcopy(random_filename))
time.sleep(0.05)
if event.getKeys([‘r’]):
win.close()
if event.getKeys([‘space’]):
a1+=1
thisExp.addData(‘b’,b)
print(a1)
if a1>4:
break