文件路径转义问题

1.字符串中\表示转义,比如\n表示换行,\t表示制表符Tab, \u表示后面是Unicode编码,
2.转义带来的问题:路径中如果有\u,被转义后就不是原来所写的路径了
3.3种解决的办法:

1)在路径前面加r或R: r'd:\a.txt'
file = open(r"C:\Users\acer-pc\Desktop\test.txt")
res = file.read()
print(res)

2)路径使用/,这也是Linux中的路径写法: 'd:/a.txt'
file = open("C:/Users/acer-pc/Desktop/test.txt")
res = file.read()
print(res)

3)路径使用\: 'd:\a.txt'
file = open(r"C:\Users\acer-pc\Desktop\test.txt", "w")
res = file.write("today is a good day, bad thing is i miss him ,althongh i know he doesn't like me")

猜你喜欢

转载自www.cnblogs.com/come202011/p/12229101.html
今日推荐