Jeff Saltfist
Jeff Saltfist

Reputation: 943

WindowsError trying to get directory listing with os.listdir()

Trying to list files in a directory:

H:\Photos\images\6811957

When I run my script:

print "Files in path",os.listdir(direc)

I get a syntax error:

Traceback (most recent call last):
File "H:\Photos\images\read_RGB.py", line 34, in <module>
print "Files in path",os.listdir(array)
WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'H:\\Photos\\images\\6811957\n/*.*'

Is there a quick fix? Not sure why the added characters are occurring.

Upvotes: 0

Views: 135

Answers (1)

Afaq
Afaq

Reputation: 1155

I think \6 is causing an issue here. I just tried printing the path in IDLE and I got this.

>>> print('H:\Photos\images\6811957')
H:\Photos\images811957

Try doing the following when defining the path value

>>> print(r'H:\Photos\images\6811957')
H:\Photos\images\6811957

Added r before path to treat it as a raw text.

Upvotes: 1

Related Questions