Python open file unicode error

前端 未结 1 1229
忘掉有多难
忘掉有多难 2020-12-19 12:57

I\'m learning how to open a file in Python, but when I type the path to the file I want to open, a window pops up, saying \"(unicode error) \'unicodeescape codec can\'t deco

相关标签:
1条回答
  • 2020-12-19 13:26

    One obvious problem is that you're using a normal string, not a raw string. In

    open ("C:\Users\Rajrishi\Documents\MyJava\text.txt") 
                                             ^^
    

    the \t is interpreted as a tab character, not a literal backslash, followed by t.

    Use one of the following:

    open("C:\\Users\\Rajrishi\\Documents\\MyJava\\text.txt") # meh
    open(r"C:\Users\Rajrishi\Documents\MyJava\text.txt")     # better
    open("C:/Users/Rajrishi/Documents/MyJava/text.txt")      # also possible
    
    0 讨论(0)
提交回复
热议问题