python raw字符串抑制转义

#字母r(大写或者小写)出现在字符串点第一个引号前面,它将关闭转义机制
print(r'c:\new\text.dat')
print('c:\\new\\text.dat')
#这两句代码的效果相同

#print(r'c:\new\text.dat\')   #报错,不能以单个反斜杠结尾,必须要转义SyntaxError: EOL while scanning string literal
print(r'c:\new\text.dat\\'[:-1])   #如果最后必须是以反斜杠结尾,则可使用该方法
print(r'c:\new\text.dat'+'\\')     #如果最后必须是以反斜杠结尾,则可使用该方法
print('c:\\new\\text.dat\\')     #如果最后必须是以反斜杠结尾,则可使用该方法

'''output
c:\new\text.dat
c:\new\text.dat
c:\new\text.dat\
c:\new\text.dat\
c:\new\text.dat\
'''

猜你喜欢

转载自blog.csdn.net/weixin_37016700/article/details/78623504
今日推荐