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.

1

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

1
import glob
my_new_list = glob.glob("/your/folder/*")

1 Comment

So concise, I love it. Thank you. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.