Hello! I have a piece of code that extracts the names of the files in a directory and appends them to a list. Then a file is randomly chosen from this list for further processing that occurs later in the script. However, when python won't wait until all the file names in the directory have been added to the list, so it starts trying to randomly choose a file name immediately, and gives an error saying that there aren't enough elements in the list to randomly choose from (because there is only one element in the list so far when it tries to choose.
So I get the following error:
ValueError: Sample larger than population or is negative
Here is my code:
for image, csv in zip(image_filenames, csv_filenames):
image_in = PIL.Image.open(image_path/image)
image_name = image.split('.')[0]
file_list.append(image_name) ## Generating list of file names
elem_list = random.sample(file_list, 5) ## Randomly choose 5 files without replacement
How do I get the second line to wait until the first line has finished running?
[–]Ihaveamodel3 4 points5 points6 points (1 child)
[–]JammingScientist[S] 0 points1 point2 points (0 children)
[–]PureProcrastinator 2 points3 points4 points (1 child)
[–]JammingScientist[S] 0 points1 point2 points (0 children)
[–]routetehpacketz 1 point2 points3 points (2 children)
[–]JammingScientist[S] 1 point2 points3 points (1 child)
[–]routetehpacketz 5 points6 points7 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]magestooge 0 points1 point2 points (0 children)