Home > OS >  Simple code with PIL library in Python is not workig
Simple code with PIL library in Python is not workig

Time:02-05

Well, first time in writing in stackoverflow. when I depure and run this code

from PIL import Image
import os

downloadsFolder = "\Users\fersa\Downloads"
picturesFolder = "\Users\fersa\OneDrive\Imágenes\Imagenes Descargadas"
musicFolder = "\Users\fersa\Music\Musica Descargada"

if __name__ == "__main__":
    for filename in os.listdir(downloadsFolder):
        name, extension = os.path.splitext(downloadsFolder   filename)

        if extension in [".jpg", ".jpeg", ".png"]:
            picture = Image.open(downloadsFolder   filename)
            picture.save(picturesFolder   "compressed_" filename, optimize=True, quality=60)
            os.remove(downloadsFolder   filename)
            print(name   ": "   extension)

        if extension in [".mp3"]:
            
            os.rename(downloadsFolder   filename, musicFolder   filename)

I get this message on terminal SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape PS C:\Users\fersa\OneDrive\Documentos\Automatizacion Python> but i don't know what it means

I tried chanching the files directory many times but it doesn't work

CodePudding user response:

Add an r before each string to signify that it's a raw string without escape codes. Currently, every \ character is telling python to try and interpret the next few bytes as a unicode character:

from PIL import Image
import os

downloadsFolder = r"\Users\fersa\Downloads"
picturesFolder = r"\Users\fersa\OneDrive\Imágenes\Imagenes Descargadas"
musicFolder = r"\Users\fersa\Music\Musica Descargada"

if __name__ == "__main__":
    for filename in os.listdir(downloadsFolder):
        name, extension = os.path.splitext(downloadsFolder   filename)

        if extension in [".jpg", ".jpeg", ".png"]:
            picture = Image.open(downloadsFolder   filename)
            picture.save(picturesFolder   "compressed_" filename, optimize=True, quality=60)
            os.remove(downloadsFolder   filename)
            print(name   ": "   extension)

        if extension in [".mp3"]:
            
            os.rename(downloadsFolder   filename, musicFolder   filename)

You can read more about string prefixes in the python docs

CodePudding user response:

The cause of the SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape is the code line:

downloadsFolder = "\Users\fersa\Downloads"

in which "\U" is telling python to interpret the next 8 characters as a hexadecimal value of an Unicode code point. And because in "\Users\fes" are characters not being 0-9,A-F,a-f there is an Error which won't occur if the string would start for example with "\Uaabbaacc\somefilename" making it then harder to find out why no files can be found.

The options for a work around fixing the problem are:

  • usage of forward slashes instead of backslashes: "/Users/fersa/Downloads".
  • definition of the string as a raw string: r"\Users\fersa\Downloads" in order to avoid interpretation of \U as an escape sequence

Check out in Python documentation page the section 2.4.1. String and Bytes literals for more about escape sequences in Python string literals.

  • Related