How to display a random picture from a folder (Python) -
i have display random image folder using python. i've tried
import random, os random.choice([x x in os.listdir("path")if os.path.isfile(x)]) but it's not working me (it gives windows error: wrong directory syntax, though i've copied , paste).
which problem?
you need specify right relative path:
random.choice([x x in os.listdir("path") if os.path.isfile(os.path.join("path", x))]) otherwise, code seek find files (image.jpg) in current directory instead of "path" directory (path\image.jpg).
update
specify path correctly. escape backslashes or utilize r'raw string literal'. otherwise \.. interpreted escape sequence.
import random, os path = r"c:\users\g\desktop\scientific-programming-2014-master\scientific-programming-2014-master\homework\assignment_3\cifar-10-python\cifar-10-batches-py" random_filename = random.choice([ x x in os.listdir(path) if os.path.isfile(os.path.join(path, x)) ]) print(random_filename) python
No comments:
Post a Comment