LPTHW - Exercise17 - Strange Error

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print(f"Copying from {from_file} to {to_file}")

in_file = open(from_file)
indata = in_file.read()

print(f"The input file is {len(indata)} bytes long")

print(f"Does the output file exist? {exists(to_file)}")
print("Ready, hit RETURN to continue.")

out_file = open(to_file, 'w')

print("Alright, all done.")


l> write-output "This is a test file." > test.txt
> get-content test.txt
This is a test file.
> python ex17.py test.txt new_file.txt
Copying from test.txt to new_file.txt
Traceback (most recent call last):
  File "ex17.py", line 10, in <module>
    indata = in_file.read()
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python37-32\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'CP_UTF8' codec can't decode bytes in position 0--1: No mapping for the Unicode character exists in the target code page.

I don’t understand why this error is coming up

Never mind I worked it out it saved as utf-16 rather then utf-8 so I changed it now it works.

It’s funny how often the traceback tells you exactly what the the problem is but it takes a whilst to sink in.

I had similar yesterday where for a hour I kept hitting an error telling me it was expecting ‘as’ but getting something else.

After going through each potential error page and double-checking syntax, I realised I had forgot ‘as’ during the flask-bootstrap import statement even though I’d written it about 15 times on other pages:

{{ import ‘bootstrap/wtf.form’ as wtf }}

The clues are almost always there in the stacktrace but some time away from the screen, or writing up the bug as Zed suggests, often helps bring it into view.


Wise words, I also wasn’t sure if i should delete my whole post. Then I thought what if someone gets the same problem maybe I should leave it up.

1 Like

Definitely leave it up. :slight_smile:

It’s been a while and I took a long break from this, but I hope to finish all the exercises this time round. I got the same error it’s strange how powershell and windows systems still haven’t updated to using utf-8 despite the code page being 65001 it still has many errors, and also so when you create a txt file using echo it saves it automatically as utf-16 since the windows code page is 1200.

I assume Windows defaults to utf-16 now due to the common use of emojis and all the other gumpf that people communicate with. I’d just move on from that exercise as it is a bit lengthy for limited value at that stage.

1 Like

Yeah I’m up to exercise 20 now so I’m getting there. There is no resolving the windows encoding issue I wonder if it’s worth learning Linux or an another OS that won’t have this issue with utf-8. I did find this site https://tabby.sh/ for a terminal to download that has full Unicode support with double-width characters.

For most of the book, you could get away with using https://replit.com/ or similar if you want to avoid the overhead of setup. Might be worht a look.

1 Like

Yes, I vaguely remember that windows has weird conflicts where some things make utf-16 files and then things like powershell can’t display them.

If you want to do it over just make the file with your text editor. Let me know if that works, or just move on like @gpkesley says.

1 Like
A free service run by Zed A. Shaw for learncodethehardway.org.