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

Time:06-04

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.

CodePudding user response:

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

print(r'.\numbers.txt')

Or escape the backslashes with \\

print('.\\numbers.txt')

CodePudding user response:

You need to escape the escape character. So

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

This should work for your needs.

Thanks Scott.

  • Related