Ex23 Program won't run - Tried Power Shell & Cmder

I can’t get the program to run. I’ve tried using both Win Power Shell and Cmder.

@stdojo
no error message is interesting, the command looks accurate, can we see your code

Arrrhhh… Found my error. Left out some code. Thank you for responding. New problem though:

Here’s my code:

import sys
script, input_encoding, error = sys.argv


def main(language_file, encoding, errors):
    line = language_file.readline()

    if line:
        print_line(line, encoding, errors)
        return main(language_file, encoding, errors)


    def print_line(line, encoding, errors):
        next_lang = line.strip()
        raw_bytes = next_lang.encode(encoding, errors=errors)
        cooked_string = raw_bytes.decode(encoding, errors=errors)

        print(raw_bytes, "<===>", cooked_string)

languages = open("languages.txt", encoding="utf-8")

main(languages, input_encoding, error)

Thank you!

1 Like

@stdojo
can you reformat your code post and test this code again, as I reformatted and it worked for me, which implies the fault is in your setup, but we just need to confirm that first

Thanks for your help!
Here’s a print screen of Visual Studio Code with my code. This forum won’t let me upload anything but images.


And here’s the text again:

import sys
script, input_encoding, error = sys.argv


def main(language_file, encoding, errors):
    line = language_file.readline()

    if line:
        print_line(line, encoding, errors)
        return main(language_file, encoding, errors)


    def print_line(line, encoding, errors):
        next_lang = line.strip()
        raw_bytes = next_lang.encode(encoding, errors=errors)
        cooked_string = raw_bytes.decode(encoding, errors=errors)

        print(raw_bytes, "<===>", cooked_string)

languages = open("languages.txt", encoding="utf-8")

main(languages, input_encoding, error)

Thank you!
Steve

@stdojo
ok here’s your code back that works and the formatting should look right

import sys
script, input_encoding, error = sys.argv

def main(language_file, encoding, errors):
    line = language_file.readline()

    if line:
        print_line(line, encoding, errors)
        return main(language_file, encoding, errors)


def print_line(line, encoding, errors):
    next_lang = line.strip()
    raw_bytes = next_lang.encode(encoding, errors=errors)
    cooked_string = raw_bytes.decode(encoding, errors=errors)

    print(raw_bytes, "<===>", cooked_string)

languages = open("languages.txt", encoding="utf-8")


main(languages, input_encoding, error)

does this code give you that same error?

To format the code in the forum, if you do the forum badges you’ll see how it’s done and other stuff
edit, I think it’s the certified badge that shows you how to add code to posts, but I cannot find the tutorial at the moment

Thank you very much Jason. I just got home and ran it with your code. Here’s what I got:

@stdojo
The code works here, have tried on 2 machines one with 64bit python and the other 32bit which I’m guessing your using from the path in the error.
We’ve proved your code is good, looking at the error message “invalid continuation byte” this could be referring to your language.txt file and it has a corruption in the file or your python install and the file called codecs.py has been altered and cannot now handle the language.txt file, do you recall changing codecs.py file, I suspect not.

Could you download the languages.txt file again and run against this ?

ps how you getting on with VS code, I’m trying to use it, setup is easy on Windows, have struggled getting it working on Linux Mint then the .vscode folder and path to python not being local and using the .vscode folder in the code folder and not the local folder meaning the python path is incorrect, challenges challenges eh :smiley:

1 Like

Hi Jason!
Wow, thanks so much for your help.
Yes, I downloaded the file (languages.txt) again, ran ex23.py and got the expected results. Image attached.
Yes, VS Code is working great for me. LPTHW encourages the use of various editors, but I really like the way that VS Code displays the indenting for the Python code. That has tripped me up many times. I am working through codecademy/Python too and I don’t like their interface at all. I often will copy and past exercises into VS Code.
Wow, your really pushing it with the Linux stuff. I would like to get into that too. I have some experience in Linux but it was many years ago and I have forgotten most of it.
What programming languages do you know?
Thanks again Jason!

@stdojo
excellent we got there in the end, I didn’t know the answer but my many years (ago) programming in VB 4 then 6 taught me about stepping through problems and controlling the unknowns.

have a look at https://www.edx.org/course/introduction-computer-science-mitx-6-00-1x-11
I undertook the course when it was time limited to two & half months, what a treadmill but such a sense of accomplishment at the end. I only wish I’d found and taken LPTHW before the course it would have helped so much.

I’m getting so frustrated with the propriety stuff, the lock in and constant upgrade and direction change hence looking at Linux and Python but it’s hard going, all my current clients are Windows based, I think save this rant for another sub as I’m going off topic and don’t want to ruin this great forum.
jason

1 Like

Thanks for helping @stdojo out @jason.

Thanks @jason i’ve been stuck on this ex with the same error as @stdojo for over a week trying to figure out what’s going on and trying to fix PS! I’m gonna be on this forum a lot more!

And thanks @stdojo for posting this!

Cheers!

So @stdojo can you post a description of the solution to this problem? Was it that the first download of languages.txt was incorrect?

Okay, so here’s the solution. I also had the very same problem. I had downloaded the “languages.txt” file on my phone and when I transferred it to my system, it was like this:

So it showed the same error as above (2nd image from @stdojo).
Apparently, I downloaded the file from my PC and the file was like this:

This time the program ran. Well, I’m new to python so I don’t know how these two files were interpreted in different ways. The only thing that matters is it worked. But the special characters still appear as question marks. Can anyone help?