Getting participant's ID on code

Hi, I’m trying to run an experiment and create a results txt file in every session with a classical open write python code:

resultats = open(‘training_resultats.txt’, ‘w’)

At the beginning of the experiment there’s this screen to write the participant’s ID. How could I implement this on the name of the file? Some sort of ‘training_resultats(’ + input + ‘).txt’ so the file don’t overwrite and I can keep track of the different results?

Thanks to all!

Captura de pantalla 2021-04-15 a les 22.58.50

It is accessible as:

expInfo['participant']

Is there some reason why you need to save your own data file rather than rely on PsychoPy’s built-in mechanism to do that for you automatically?

Thanks, Michael! Yes, there is. I am putting the information in a certain way (number of routine, button they pressed, reaction time) and I have the right answers in another txt (organized the same way) so I can put them together on python and check the right/wrong answers with a few more statistics (reaction time and sort of button they pressed according to stimuli).

OK, but PsychoPy also can handle that automatically for you. i.e. if the correct responses are specified in a conditions file that is linked to a loop, then all the values from that file are automatically incorporated into the data output file row for that trial, along with the corresponding measured responses and reaction times. If using the keyboard component, it can also then calculate whether responses were correct or not. Custom fields that you still might might need to calculate using your own Python code can then be easily added to the data output file with just a single expression:

thisExp.addData('some_column_name', some_calculated_value)

with all the other housekeeping (naming and opening the file and storing all of the other info for each trial) done for you automatically. The PsychoPy data file will automatically capture a whole swag of variables (e.g. things related to timing performance) that you might not consider important, but could become so later…

1 Like