0

I have the following chunk of code:

for i in range(len(files)):
    if files[i].split('^')[0]=='SequenceFile':
        number = files[i].split('^')[5]
        file = files[i].split('^')[2]
        src_path = f"C:\Users\User\Desktop\SequenceFolder\{number}\{file}"
        dst_path = f"C:\Users\User\Desktop\FinalFolder

I am getting returned the following error:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

I realize that this is because of the addition of {number} and {file} in my code, but I cannot seem to figure out how to add variable names to something like this. Any chance anyone has an idea?

Thanks!

  • 2
    Prefix both of your path strings with `rf` (or `fr`): `r` to avoid interpreting backslashes as escape sequences, `f` to perform the variable substitutions. – jasonharper Oct 10 '22 at 15:28
  • use src_path = f"C:\\Users\\User\\Desktop\\SequenceFolder\\{number}\\{file}" – roshan ok Oct 10 '22 at 15:30
  • Or just use forward slashes: `src_path = f"C:/Users/User/Desktop/SequenceFolder/{number}/{file}"`. And yes, this will work on a Windows OS. – Matthias Oct 10 '22 at 15:44

0 Answers0