| Reference | Downloads | Github

The location of output data file

Hi all,

Would it be possible to make the experiment generate the output data file in a specific place?

For example, I have a folder named (Chinese Participants), and I want the output data file is automatically located inside this folder.

Your help is really appreciated.

Click the Experiment Settings button on the toolbar.

Thank you Michael a lot.

I clicked on it, but still can’t know how to change the location of output data file.

Could you please give a bit more information ?

Best regards,

Hi Sami,

I don’t think you can actually do what you want to do from within the builder. But, it’s easy to do.

For this to work, I assume you built your experiment with the builder, and you didn’t alter anything in the code.

Go to the builder view of your experiment, and click on the “compile script” button.

You should see a screen with all the code for your experiment.

Find the line that says,

#Data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
filename = _thisDir + os.sep + u'data/%s_%s_%s' %(expInfo['participant'], expName, expInfo['date'])

This tells psychopy where to save your data files.

I assume you want to save your files in some place other than where you start up your experimental script. For example, my you want to save in the folder,


then you could remove

_thisDir + os.sep + u'data/

from the filename variable, and replace it with,

"/users/sami/desktop/samisExperimentData/" + expInfo['participant'] + expName + expInfo['date']

I would recommend keeping all of the details about this particular experimental run, i.e.

 expInfo['participant'] + expName + expInfo['date']

I would further recommend you normalize the filename variable with the os.path.normpath(path) function. You can read about it here. Your new filename variable in the code is thus:

filename = os.path.normpath("/users/sami/desktop/samisExperimentData/" + expInfo['participant'] + expName + expInfo['date'])

For this to work properly, I assume you have already the file at the location,


Also, if you wanted to actually create a file for each new participant, then you could similarly hack your way to it. Just use the function, os.mkdir(path), which you can read about here.

Good luck!

If I may make a suggestion, I think it would be better to use @Michael 's suggestion, since after compiling the code, you won’t be able to go back to the Builder interface. A third option would be to use a code component.

The first is to change the Experiment settings as Michael suggested. One thing you have to remember is, whatever you put in the “Data filename$” field is put after a call to _thisDir, meaning that it is always calculated in relation to where your experiment .psyexp file is. So if your “ChineseParticipants” folder (I recommend you remove the space in the name) is in the same folder as your .psyexp file, this is an easy fix:

u'ChineseParticipants/%s_%s_%s' % (expInfo['participant'], expName, expInfo['date'])

This is probably the easiest and best solution: I think it’s a good idea to have your data be in a subfolder of your experiment, so that later you can easily know what data belongs to what experiment.

The second easiest (but less flexible, and honestly more dangerous solution) would be to leverage the fact that “…/” means go one folder up. So if I have my experiment in a folder call “Experiment” in my Documents folder, and I want to save data on the Desktop, I could do this by going two folders up, then going into the Desktop folder:

u'../../Desktop/ChineseParticipants/%s_%s_%s' % (expInfo['participant'], expName, expInfo['date'])

The downside here is that if you ever move your experiment to a different folder and forget to change this, you will get unexpected results.

@Sami_Alsalmi if you’re not understanding what these things mean, lookup “file paths”, and “python string formatting %s”.

The third option is similar to Dan’s (@cookdj0128 ) solution, but I would put it in a code component that runs at the very beginning of the experiment. If you’re interested in this let me know, but for future reference, the general idea is to reset the filename attribute of the Experiment handler and the log file, something like this:

newFileName = os.path.join(os.path.expanduser(u"~"), u"Desktop", u"ChineseParticipants")
thisExp.filename = newFileName
logFile.filename = newFileName
1 Like