How to use \ in a string without it's escape effect

Advertisements

I’m trying to implement the next line:

with open('.\numbers.txt', 'r') as f:

The \n in the path is being recognized as the new line special character.

>Solution :

You can use the raw string by prefixing the string with r

print(r'.\numbers.txt')

Or escape the backslashes with \\

print('.\\numbers.txt')

Leave a ReplyCancel reply