기계는 거짓말하지 않는다

Python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 8-9: truncated \UXXXXXXXX escape 오류 본문

Python

Python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 8-9: truncated \UXXXXXXXX escape 오류

KillinTime 2023. 4. 29. 15:18

보통 파일 경로를 읽을 때 발생할 수 있다.

경로 상에 \ (back slash)로 기입되었을 경우

경로 상에 \ (back slash)를 \\ 두번 적어주거나 / (slash)로 바꿔주면 해결할 수 있다.

또는 raw string을 명시해서 해결할 수 있다.

# 1
path = "F:\\dir\\AA.txt"

# 2
path = "F:\dir\AA.txt"
path = path.replace("\\", "/")

# 3
path = r"F:\dir\AA.txt"
Comments