0
I’m trying to open a file with data .txt
in Python but an error message appears.
arq = open('C:\Users\Cintia\Documents\Python\Dados\lbe.txt', 'r')
lbe = arq.read()
print(lbe)
arq.close()
The mistake is unicodeescape
.
0
I’m trying to open a file with data .txt
in Python but an error message appears.
arq = open('C:\Users\Cintia\Documents\Python\Dados\lbe.txt', 'r')
lbe = arq.read()
print(lbe)
arq.close()
The mistake is unicodeescape
.
5
The path of a file in Windows generates a conflict in Python due to the backslash. When you use, for example, C:\Users
, Python will interpret the character \U
as an escaped U, similar to what occurs with the \n
to break line.
To circumvent this error, you need to prefix the string with a r
, to indicate to the Python interpreter that the string should be analysed "raw" (the r
is of raw string), being like this:
arq = open(r'C:\Users\Cintia\Documents\Python\Dados\lbe.txt', 'r')
3
The problem is with the string 'C:\Users\Cintia\Documents\Python\Dados\lbe.txt'
.
\U
begins an 8-character Unicode escape and, as is succeeded by s
, which is an invalid sequence, bursts the error. The same would happen with other sequences, such as \r
, \n
, \t
and so on. Therefore, whenever you represent a backslash, it is good to duplicate it, causing it to be escaped too. So you will always be sure that the value produced will be just one \
.
This can be solved by using two backslashes to represent paths in Windows.
arq = open('C:\\Users\\Cintia\\Documents\\Python\\Dados\\lbe.txt', 'r')
1
The problem may be in the U of "C: Users". Perhaps, you should mention that the path is a rawstring. Try some of the options below:
Example 1:
arq = open(r'C:\Users\Cintia\Documents\Python\Dados\lbe.txt', 'r')
Or else:
arq = open'''C:\Users\Cintia\Documents\Python\Dados\lbe.txt''', 'r')
Browser other questions tagged python
You are not signed in. Login or sign up in order to post.
This data that I imported, it is possible to do Plot, because a sequence of errors appears. I thank you already
– Cynthia Silva
I don’t understand what you mean. Which Plot? Do you need some kind of chart? Maybe it’s a question for another question.
– Woss