Search code examples
pythonpython-2.7directoryoperating-systemwindowserror

How to open a random Image from a given file directory?


I am trying to open the image from this directory but am not been able to. It gives me the following error:

WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect.

This is my code:

import os, random
random.choice(os.listdir("C:\\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"+'png'))

In here , in the folder backgrounds there are many images.I want a random Image to open.But while running the program , I am getting WindowsError.

What am I doing wrong?

Edit 1

I tried this:

random.choice(os.listdir(r"C:\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"+'png'))

But am getting the error:

WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'C:\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds\*.png/.'

Edit 2

I tried this:

import os, random

a=random.choice(os.listdir(r"C:\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"))
os.open(a)

Now I dont get error but it does not open the image either.

Edit 3

I have also tried:

 import random,os
folder= "C:\\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"
a=random.choice(os.listdir(folder))
print(a)

from PIL import Image
file = folder+'\\'+a
Image.open(file).show()

    #os.open(a, os.O_RDWR)
    from PIL import Image
    file = folder+'\\'+a
    Image.open(file).show()

But got the following error once again:

Traceback (most recent call last): File "G:\Grade 12 Project\auto.py", line 4, in a=random.choice(os.listdir(folder)) WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'C:\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca'

Here(the yellow highlighted part) is the directory where my images are stored.

Image of Directory


Solution

  • Use the PIL instead.

    import os, random
    
    folder=r"D:\Study\SO"
    
    a=random.choice(os.listdir(folder))
    print(a)
    
    #os.open(a, os.O_RDWR)
    from PIL import Image
    file = folder+'\\'+a
    Image.open(file).show()
    

    source: Open and display .png file in python using PIL


    The problem with this is that a doesn't have absolute path to that chosen random files.

    1. In Edit 1, the "png" gets concatenated but there is no folder named "backgroundspng"
    2. In Edit 2, you haven't given the flags for os.open() which can be found here.
    3. In Edit 3, please make sure that you are using the r before string.

    In you case, use this :

    folder = r"C:\\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"