mardi 14 juillet 2015

How can I open a series of files (PNGs) from a specified directory (randomly) using Python?

I have a folder in a specified directory containing several PNGs that need to be opened randomly. I can't seem to get random.shuffle to work on the folder. I have thus far been able to print the contents, but they need to also be randomized so when they are opened, the sequence is unique.

Here's my code:

import os, sys
from random import shuffle

for root, dirs, files in os.walk("C:\Users\Mickey\Desktop\VisualAngle\sample images"):
    for file in files:
        if file.endswith(".png"):
            print (os.path.join(root, file))

This returns a list of the images in the folder. I'm thinking maybe I could somehow randomize the output from print and then use open. I have so far failed. Any ideas?




Aucun commentaire:

Enregistrer un commentaire