Wakefield's Daily Tips

PsychoPy files are saved in plain text

When you save your experiment in Builder the saved .psyexp file is a script that can be opened in a text editor. This can be particularly helpful if your .psyexp gets corrupted or if you want to search and replace a variable name. The Python and JavaScript files are also saved in plain text but see Wakefield's Daily Tips - #17 by wakecarter

Here’s an example of a PsychoPy Coder script that you can use to search for a particular word in the psyexp files across all of your experiment folders. [I should use this more often myself].

# https://stackoverflow.com/questions/4205854/recursively-find-and-replace-string-in-text-files
import os, fnmatch
def search(directory, find, filePattern):
    for path, dirs, files in os.walk(os.path.abspath(directory)):
        if path.count(os.path.sep) == directory.count(os.path.sep) + 1: # One more than \\ in directory
            for filename in fnmatch.filter(files, filePattern):
                print(filename)
                filepath = os.path.join(path, filename)
                try:
                    with open(filepath) as f:
                        s = f.read()
                    if s.find(find,0) > -1:
                        print("Found " + find + " at position " + str(s.find(find,0)))
                except:
                    print("Failed to open",filename)

#This allows you to do something like:
search("C:\\Users\\userName\\Documents\\Pavlovia", "searchTerm", "*.psyexp")

Note the double \ between folders in the path

1 Like