3

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

White Shadow
  • 444
  • 2
  • 10
  • 26

2 Answers2

4

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" 
Community
  • 1
  • 1
ABcDexter
  • 2,751
  • 4
  • 28
  • 40
0

You need to use a raw string by prepending the string with r:

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

Else, you would need to escape each backslash by another backslash.

Pierre Barre
  • 2,174
  • 1
  • 11
  • 23
  • Hey! I am getting this error now `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 dont know what's wrong? I just want it to open any random image from the directory I gave. – White Shadow Dec 07 '16 at 07:13