Program not always saving data

I have several programs running currently. The programs are running 24/7 so the animals can work whenever they choose. Overall, the programs are working quite well. Not incredibly often, but occasionally, the programs will crash or need to be force quit. When this happens, there is no data file, which is very problematic for me. The program will run without issues for several hours and the participants will do thousands of trials which are all now lost. I don’t see why psychopy would not create a data file if the program crashes or is forced quit? This has not been an issue for me in python or visual basics. Does psychopy wait to write the data files until it is closed? Is there a way to change this so the data file is created at the beginning and writes the information I have added after each trial?

This is the third time I’ve posted on this issue, I did not receive any responses on the previous posts, any advice at all would be greatly appreciated!


It seems that PsychoPy does not create a data file if the programme crashes or is forced to quit. The data file is only generated when the experiment concludes or when the programme is closed. However, there are ways to save data after each trial iteration.

One solution is to utilise the TrialHandler class in PsychoPy, which enables data to be saved after each trial iteration. An alternative solution is to incorporate a code component in the PsychoPy builder to write the data to a file after each trial iteration.

In addition, it’s feasible to store multiple sessions for a single subject or a single file for multiple subjects. This approach allows you to store multiple sessions for a single subject (using the subject as the filename and a date-stamp as the sheetname), or a single file for multiple subjects.

Hope this helps.

1 Like

I ended up putting this code in the begin experiment tab of my first routine:

import os
import datetime

current_date ="%d-%m-%Y_%H-%M-%S")
folder_name = "backup"
datafile = os.path.join(folder_name, f"{expName}_{expInfo['monkey']}_{current_date}.csv")

and then this code in the end routine tab of the last routine:

if (trials.thisN % 1) == 0:
    thisExp.saveAsWideText(datafile, fileCollisionMethod='overwrite', delim='auto')

And this seems to be working. Thank you so much for your help! I really appreciate it! I really like using psychopy since I also occasionally put experiments online for human testing, and because I am not an expert programmer by any means. I was getting worried I was going to have to switch to using python like my colleagues. But this should fix the last problem I was having!

1 Like

I see, it’s wonderful to know you’ve found a solution that works well for your needs! PsychoPy can indeed be very user-friendly. Please feel free to share your experiences or any further queries you may have. Happy experimenting!