[Error]’unicodeescape’ codec can’t decode bytes
Problem
When I run the python code below, the error “SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3” occurred.
import glob import os word = "single_post_title" path = "C:\Users\USERNAME\Downloads\document" files = glob.glob(path +'\**') for f in files: print(f)
Environment
Windows10
Python 3.8.6
Cause of the error
The unicodeescape ‘\’ is used in path = “C:\Users\USERNAME\Downloads\document”. “\” is used as a part of escape sequences in Windows system.
Solution
Solution 1. Use “\\” to express “\” in string data
path = "C:\\Users\\USERNAME\\Downloads\\document" files = glob.glob(path +'\\**')
Solution 2. Use “/” instead of “\”
path = "C:/Users/USERNAME/Downloads/document" files = glob.glob(path +'/**')