zoukankan      html  css  js  c++  java
  • 解决Python打开文件---路径报错问题(SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape)

    原因分析:

    在windows系统当中读取文件路径可以使用\,但是在python字符串中有转义的含义,

    如 可代表TAB, 代表换行,

    所以我们需要采取一些方式使得不被解读为转义字符。目前有3个解决方案

    1、在路径前面加r,即保持字符原始值的意思。

    1 file = r'C:UsersAdministratorDesktopworld_data.txt'

    2、替换为双反斜杠

    1 file = r'C:\Users\Administrator\Desktop\world_data.txt'

    3、替换为双正斜杠

    1 file = r'C://Users//Administrator//Desktop//world_data.txt'

    4、替换为单正斜杠

    1 file = r'C:/Users/Administrator/Desktop/world_data.txt'

     Tomorrow the birds will sing.

  • 相关阅读:
    12.4
    12.3
    学期总结
    12.20 期末考试总结
    12.18 准备
    12.17 CSS学习
    12.16 CSS学习
    12.15 CSS学习
    12.14 学习总结
    12.11 AJAX的基础知识
  • 原文地址:https://www.cnblogs.com/rainbow-1/p/14576558.html
Copyright © 2011-2022 走看看