7

My code:

red_images =  'DDtest/210red.png'
green_images = 'DDtest/183green.png'

b = [red_images, green_images]
shuffle(b)

I have several hundred pictures, and in hopes of making my code as concise as possible (and to expand my python knowledge), I was wondering how I write code that automatically takes the files in a folder and makes them a list.

I have done similar things in R, but I'm not sure how to do it in python.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
y3trgfhsfgr
  • 467
  • 8
  • 17

2 Answers2

3

You can also use os:

import os
from random import shuffle

# Base directory from which you want to search    
base_dir = "/path/to/whatever"

# Only take the files in the above directory
files = [f for f in os.listdir(base_dir) 
         if os.path.isfile(os.path.join(base_dir, f))]

# shuffle (in place)
shuffle(files)
1
import glob
my_new_list = glob.glob("/your/folder/*")
RustyShackleford
  • 25,262
  • 6
  • 22
  • 38