-1

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

2 Answers2

-1

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

Adid
  • 1,504
  • 3
  • 13
  • I actually find out that the problem is that I am using forward slashes (/) instead of backslashes () to separate directories in a file path, especially this is if you're using a Windows operating system. So, the code will be like this only for the variables of my directories: downloadsFolder = "/Users/fersa/Downloads/" picturesFolder = "/Users/fersa/OneDrive/Imágenes/Imagenes Descargadas/" musicFolder = "/Users/fersa/Music/Musica Descargada/" – Fernando Sanchez Feb 05 '23 at 05:40
  • That works too! Python can use both forward and backslashes depending on your operating system. Using the r prefix can help if you want to use backslashes. – Adid Feb 05 '23 at 05:41
  • The statement *every \ character is telling python to try and interpret the next few bytes as a Unicode character* needs a small adjustment: The "\U" escape sequence is the reason for the Error as the escape sequence. `\U` is telling python to interpret the next 8 characters as a hexadecimal value of an Unicode code point. And because in "\Users\fe" 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. – Claudio Feb 05 '23 at 05:53
-1

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.

Claudio
  • 7,474
  • 3
  • 18
  • 48