Score:-2

Python, problem reading lines in open file using "open('file_name.txt', 'r')"

in flag

I'm trying to read the content of a text file, line by line, with this code:

import os.path  

file_to_read = open("file_name.txt", "r")  
lines = file_to_read.readlines()

When I run it I get the following error:

enter image description here

Traceback (most recent call last):
File "D:/Files/test.py", line 4, in < module>
lines = file_to_read.readlines()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 260: character maps to < undefined>

If I remove the last line, so the code looks like this:

import os.path  

file_to_read = open("file_name.txt", "r")  

Then I don't get any errors.

enter image description here

Which points me towards a problem with lines = file_to_read.readlines(), but I cannot see anything wrong with it.

Ola Ström avatar
in flag
Found a good answer here as well: https://stackoverflow.com/questions/491921/unicode-utf-8-reading-and-writing-to-files-in-python
cocomac avatar
cn flag
What operating system are you using? It appears your question [is off-topic here](https://askubuntu.com/help/on-topic) because you are not using Ubuntu or an official derivative. I see `D:/Files`, which is a Windows-style path, and your screenshot of IDLE looks like Windows.
Ola Ström avatar
in flag
Sorry, forgot about that, I'm using both Windows and Ubuntu interchangeably so it just happened to be from Windows right now. My Ubuntu that I use is on a VM on the same Windows computer...
Score:2
ug flag

There's nothing wrong with lines = file_to_read.readlines(), but this is the line that actually reads the contents of the file. open() just opens it and does not proceed to read it.

Like the error tells you, python doesn't know how to decode byte 0x9d in position 260. Check the file encoding and also make sure the file isn't corrupt.

This answer may help you, too, (i.e., explicitly specify utf-8 encoding or whatever encoding the file uses). Essentially,

with open("file_name.txt", "r", encoding="utf-8") as file_to_read:
    ...
Ola Ström avatar
in flag
'file_to_read = open("file_name.txt", mode="r", encoding="utf-8")' solved the problem...
Score:1
in flag

This question is off-topic here. You are running in Windows and asking about Python but this site is for questions about Ubuntu.

Having said that, check the text file and the position given in the error for a special character that is not part of utf-8. Something like a copyright symbol, for example.

Ola Ström avatar
in flag
Sorry, forgot about that, I'm using both interchangeably so I just happened to run the code on Windows right now. My Ubuntu is on a VM on my Windows...
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.