I want to do this by walking through the folder and randomly extracting a file from it

Help!
I am a new psychopy user and have recently encountered an unsolvable problem in programming experiments.
Here’s how my experiment went

‘ran_choice’ is given two options at random, ‘sure_choice’ requires a response from the participant, and ‘feedback_P_normal’ requires four different sound feedback (wav file) based on either ‘ran_choice’ or ‘sure_choice’. The sound feedback is placed in a folder, and I want to do this by walking through the folder and randomly extracting a file from it, but unfortunately my code doesn’t allow me to do this
Here is the code in my program that walks through folders and randomly extracts files and the error of running Times

Hi @xing , if you add your experiment code, we could suggest some fixes.

Thank you very much for your reply! I couldn’t send pictures to the forum, so I posted the code below. Because the materials are not organized yet, I will use one folder to refer to all the folders

in Begin routine:

import os

import random

global_sound = 0

file_path_Pnormal = 0

file_path_Pless = 0

file_path_Nnormal = 0

file_path_Nless = 0

**folder_path_Pnormal = “\fu”

folder_path_Pless = “\fu”

folder_path_Nnormal = “\fu”

folder_path_Nless = “\fu”

file_list_Pnormal = []

file_list_Pless = []

file_list_Nnormal = []

file_list_Nless = []

#P_normal

for root, dirs, files in os.walk(folder_path_Pnormal):

for file_name in files:

file_path_Pnormal = os.path.join(root, file_name)

file_list_Pnormal.append(file_path_Pnormal)

random.shuffle(file_list_Pnormal)

for file_path_Pnormal in file_list_Pnormal:

**print(file_path_Pnormal)

#P_less

for root, dirs, files in os.walk(folder_path_Pless):

for file_name in files:

file_path_Pless = os.path.join(root, file_name)

file_list_Pless.append(file_path_Pless)

random.shuffle(file_list_Pless)

for file_path_Pless in file_list_Pless:

**print(file_path_Pless)

#N_normal

for root, dirs, files in os.walk(folder_path_Nnormal):

for file_name in files:

file_path_Nnormal = os.path.join(root, file_name)

file_list_Nnormal.append(file_path_Nnormal)

random.shuffle(file_list_Nnormal)

for file_path_Nnormal in file_list_Nnormal:

**print(folder_path_Nnormal)

#N_less

for root, dirs, files in os.walk(folder_path_Nless):

for file_name in files:

file_path_Nless = os.path.join(root, file_name)

file_list_Nless.append(file_path_Nless)

random.shuffle(file_list_Nless)

for file_path_Nless in file_list_Nless:

**print(file_path_Nless)

in End routine

if text_dui_cuo.text ==‘√’:

if key_resp_que.keys == ‘f’:

global_sound = np.random.choice(file_path_Pnormal)

if key_resp_que.keys == ‘j’:

global_sound = np.random.choice(file_path_Pless)

if text_dui_cuo.text ==‘×’:

if key_resp_que.keys == ‘f’:

global_sound = np.random.choice(file_path_Nnormal)

if key_resp_que.keys == ‘j’:

global_sound = np.random.choice(file_path_Nless)

When you run this code, it displays an error:**

global_sound = np.random.choice(file_path_Pnormal)

File “mtrand.pyx”, line 909, in numpy.random.mtrand.RandomState.choice

ValueError: a must be greater than 0 unless no samples are taken

################ Experiment ended with exit code 1 [pid:12036]

Does this print have any files?

Is folder_path_Pnormal valid? In this code:
for root, dirs, files in os.walk(folder_path_Pnormal):
for file_name in files:

What do you get if you have a
print(file_name)
in that loop?

Also, is folder_path_Pnormal flat with no directories inside, just files?

@xing, have you considered using os.listdir() instead of walking the directory recursively? If the folder is flat with only your stimuli, then `os.listdir(“C:/Users/etc”) would return a list of files in that directory.

Yes, folder_path_Pnormal is valid, it stores my sound file, I don’t know what the meaning of file_name inside, because this is a Python friend of mine to help me write code, print(file_path_Pnormal) will eventually print all the file names in the folder

I don’t fully understand what you mean, do you mean to replace the line “for root, dirs, files in os.walk(folder_path_Pnormal):” with “os.listdir(” \fu") ", and then still run according to the previous code

In addition, I changed another method to achieve my purpose, which seemed to be simpler. I put different types of files in different excels, and then set up a new routine and loop for each type of feedback. In this way, all I need to do is execute the corresponding loop and routine under correct conditions. But my code doesn’t seem to work, which frustrates me, and I don’t know what other way to achieve my goal.

This is my code:

if text_dui_cuo ==‘√’:
if key_resp_que.corr == 1: #normal
p_normal.finished = True
P_less.finished = quit
N_normal.finished = quit
N_less.finished = quit
if key_resp_que.corr == 0: #less
p_normal.finished = quit
P_less.finished = True
N_normal.finished = quit
N_less.finished = quit
if text_dui_cuo ==‘×’:
if key_resp_que.corr == 1: #normal
p_normal.finished = quit
P_less.finished = quit
N_normal.finished = True
N_less.finished = quit
if key_resp_que.corr == 0: #less
p_normal.finished = quit
P_less.finished = quit
N_normal.finished = quit
N_less.finished = True

Yes, so instead of

for root, dirs, files in os.walk(folder_path_Nless):
    for file_name in files:
        file_path_Nless = os.path.join(root, file_name)
        file_list_Nless.append(file_path_Nless)

You could do the following:

file_list_Nless = os.listdir(folder_path_Nless)

Actually though, this will not have the full path to the file by default, so maybe you are better off with the original for loop method.

Can you show us what the print output is for the first loop:

for root, dirs, files in os.walk(folder_path_Pnormal):
    for file_name in files:
        file_path_Pnormal = os.path.join(root, file_name)
        file_list_Pnormal.append(file_path_Pnormal)
        random.shuffle(file_list_Pnormal)

for file_path_Pnormal in file_list_Pnormal:
    print(file_path_Pnormal)

In fact, this code will run in psychopy error, but on Jupyter Notebook, it can output the full file directory, I wonder what went wrong

This is result in Jupyter

This is result in psychopy

and finally, I took your advice and replaced file_list_Nless.append(file_path_Nless) with file_list_Pnormal = os.listdir(folder_path_Pnormal), but I still got an error. But fortunately the mistakes started to look different
This is the error after replaced: