0
from PIL import Image
import os

for f in os.listdir('C:\Users\diodi\Pictures'):
    if f.endswith('.jpg'):
        print(f)

i get the error

for f in os.listdir('C:\Users\diodi\Pictures'): ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

if someone can edit the error message please do.

i want to print the names of the pictures(jpg) i have in ('C:\Users\diodi\Pictures')

i am using python 3.7,i know i didn't use the pillow library yet.

  • 1
    Possible duplicate of [Why do I get a SyntaxError for a Unicode escape in my file path?](https://stackoverflow.com/questions/18084554/why-do-i-get-a-syntaxerror-for-a-unicode-escape-in-my-file-path) – smoggers Sep 03 '18 at 17:49

1 Answers1

2

The backslashes are being parsed as escape characters, use r to denote raw string

os.listdir(r"C:\Users\diodi\Pictures"):

Or escape them with more backslashes

os.listdir('C:\\Users\\diodi\\Pictures'):
Sam
  • 608
  • 4
  • 11
  • I'd say watch some YouTube tutorials, and have a look over the handbook: https://pillow.readthedocs.io/en/3.0.x/handbook/index.html – Sam Sep 03 '18 at 18:05