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?
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/.'
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.
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.
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.
os.open()
which can be found here.In you case, use this :
folder = r"C:\\Users\rkp10\AppData\Local\Google\Chrome\User Data\Default\Extensions\laookkfknpbbblfpciffpaejjkokdgca\0.91.6_0\backgrounds"