xxxxxxxxxx
'''
Windows paths for files contain backslashes which python interprets as
escape sequences. To ignore these escape sequences, you can use raw strings!
'''
import os
# os.chdir changes the current working directory
os.chdir("C:\Users\USERNAME\Desktop") # Creates error
INSTEAD, USE:
os.chdir(r"C:\Users\USERNAME\Desktop") # Add an r before the string
xxxxxxxxxx
#A raw string considers backslash as literal instead of an escape character
print(r"C\Users\MyName\Desktop")
#Without the r in front you will have to escape the \ with another \
print("C\\Users\\MyName\\Desktop")
#Both will print the same thing "C\Users\MyName\Desktop"
xxxxxxxxxx
# Add 'r' before the first quote for raw strings (ignore special characters)
print(r'C:\win32\new')