LPTHW Python 3 Ex 23 Error Raw_Bytes


Ex 23 won’t run for me. It gives me this error in Powershell:

File “ex23.py”, line 18, in
NameError: name ‘raw_bytes’ is not defined

Checked the code many times to make sure it’s exactly as in the book.


Just typed in ex 24 - same thing with ‘crates’: NameError: name ‘crates’ is not defined. I can’t find any mistakes in my typing. Is it something in my setup that’s causing this??

I’m using Notepad ++ and am starting to think this has something to do with the way Notepad ++ handles tabs, etc…

PS C:\data\python\lpthw> python ex23.py utf-8 strict
Traceback (most recent call last):
File “ex23.py”, line 23, in
File “ex23.py”, line 6, in main
line = language_file.readline()
File “C:\Program Files\Python36\lib\codecs.py”, line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: ‘utf-8’ codec can’t decode bytes in position 11-12: invalid continuation byte

Hi @Kvinneby, it looks like you probably spelled the variable raw_bytes wrong on a line before this one. Keep in mind that the code works already in the book and the chance that you’d run into a simple error like this is very low. Given that, you need to first look at your code and the biggest reason people miss these kinds of errors is they look only at the line mentioned in the error, not before it. So, it stays line 18, but look at all the lines before (17,16,15,…) for the mistake.

A quick way to find this kind of error is to simply try to find raw_bytes. If you can’t find it then you know you spelled it wrong, so look for everywhere you mention raw_bytes and make sure you spelled it exactly the same. A common mistake in this case is to spell it singular as in raw_byte.

The same goes for your error with crates as well. In fact, it looks like your most common error is spelling mistakes, so take extra care to look for those and review your code before you run it. This will help you.

Finally, you most likely clicked on the languages.txt file link in the PDF, went to it with IE, copy-pasted it to a file, and then it didn’t copy right. Instead, right click on the link and select Save As… to get the right contents of the file.

I did get it to work by switching from Notepad++ to Atom and retyping everything (and re-saving the .txt file). Then python bitched about tabs and indentation. Used Stackoverflow to find answers - DON"T USE TABS TO INDENT, apparently. I’ve stopped and started on other programming languages before and don’t remember this kind of fussiness…

Yes, I’m not sure why Atom uses TABs but you should only use spaces. The reason Python is fussy about it is that the indentation level matters for the syntax, so if you use TABs the indentation is variable and can get messed up by some text editors. It’s better to just use spaces to avoid all problems. In fact, you should just use spaces in all programming languages for that same reason.


I am using atom and still facing the same issue that is mentioned by Kvinnby

My error is as follows:

Traceback (most recent call last):
File “ex23.py”, line 21, in
main(languages, input_encoding, error)
File “ex23.py”, line 7, in main
line = language_file.readline()
File “C:\Users\SAMY\AppData\Local\Programs\Python\Python36-32\lib\codecs.py”, line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: ‘utf-8’ codec can’t decode bytes in position 11-12: invalid continuation byte

searched on stack overflow and got an answer but not sure how it worked for me:

the Line 21 in the code :
languages = open(“languages.txt”, encoding = “utf-8”)

I have replaced it by :

languages = open(“languages.txt”, encoding = “ISO-8859-1”)

and it worked for me

Your computer is a windows computer set to codec ISO-8859-1, so when you go to the link your browser converts it to ISO-8859-1. Then when you open the file it has an error.

To fix this, delete that file, then when you download it, don’t just click on it. Right-click and select “Save As…”. That seems to fix it.

Also, are you using IE or Edge? What’s your browser?