OS (e.g. Win10): macOS Tahoe 26.4.1 PsychoPy version (e.g. 2024.2.4 Py 3.8): v2026.1.3 Standard Standalone Installation? (y/n) y Do you want it to also run online? (y/n) n
Hi! I am currently designing a study that will record participants’ voice using the Microphone component. However, I would like to make PsychoPy name each audio file in a specific way. Does anyone know how to customize PsychoPy audio file name? Thank you for helping.
Hi Issac! I just tried the solution in the post you shared. However, for the “End Routine” portion of the code, PsychoPy says “/* Syntax Error: Fix Python code */”
Since I don’t have prior background in coding, I am not sure what the syntax error is. The code in the solution was:
Without seeing how it is formatted in the coding box not really. It should look something like this.
#beginning of exp
renameRecordings = []
#end of routine
old_file = os.path.join("data", micRecFolder, 'recording_mic_%s.wav' % tag)
new_file = os.path.join("data", micRecFolder, '%d%s_%s_%d.wav' % (score,Correct,expInfo['participant'],trials.thisN))
renameRecordings.append([old_file,new_file])
#end of experiment
for Idx in range(len(renameRecordings)):
# Check file size
if os.stat(renameRecordings[Idx][0]).st_size > 0:
# Rename files
os.rename(renameRecordings[Idx][0],renameRecordings[Idx][1])
else:
# Delete empty files
os.remove(renameRecordings[Idx][0])
For the end of routine code, you will need to change “micRecFolder” to “your_microphone_component_nameRecFolder”. Same with expInfo[‘participant’] is whatever that field is called (might be participant_id or something similar).
Similarly this section → (score, Correct, expInfo[‘participant’], trials.thisN) is what each of the “%s_” will be in “%s_%s_%s_%s.wav”.
So if you want just participant name and the trial number you would have:
Which would make that section of the file name “participantA1_12.wav”.
Then the “renameRecordings.append” adds the new name to the old name (which was just unix timestamp numbers).
Edit: you will also have to look at the % tag code provided by wakecarter in that thread for its usage. They may be able to provide some context on its purpose.
Hi Issac! I pasted the code into End Routine, but the code Properties window shows “Syntax Error: Fix Python Code.” I tried to see where this problem stems from, and it looks like the problem is the “.join” part of the function “os.path.join()”
Once I delete the “.join” part from line 1 and line 2, there is no longer a Syntax Error. Do you know what problem this might be–I am new to Python…
I looked at the code you provided for the tag variable in the earlier post. I wonder if I also need to add this to the code variable in my PsychoPy builder to make audio file naming work. If so, which part should I add this code to (e.g., End Routine, End of Experiment)?