OSError: [WinError 123], it adds simbols to file path and raise this error

Question:

I am trying to open a directory:

os.chdir('D:moviesClassicMusicGRANDES COMPOSITORES DA MUSICA CLASSICA17 - Verdi (Grandes Compositores da Música Clássica - Abril Coleções)')

but I get this error:

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'D:\movies\ClassicMusic\GRANDES COMPOSITORES DA MUSICA CLASSICAx0f - Verdi (Grandes Compositores da Música Clássica - Abril Coleções)'

os changes the 17 in the name of directory to x0f and raises the error.

Asked By: IgBell

||

Answers:

The backslash character ” in strings introduces an "escape sequence", it tells the python parser to change its interpretation of the following character. For example, the two characters ‘n’ get replaced by a single ‘newline’ character.

This is not what you want, so you can turn off this meaning of backslash by using a raw string, prefixed by ‘r‘:

os.chdir(r'D:moviesClassicMusicGRANDES COMPOSITORES DA MUSICA CLASSICA17 - Verdi (Grandes Compositores da Música Clássica - Abril Coleções)'
Answered By: joao