Skip to content
Advertisement

Why do i get a Syntax error using exec()?

This function grabs a python script from a paste on pastebin with the title py_0001, when the execution reaches the try: except: it throws an error SyntaxError: unexpected character after line continuation character

If you copy value of script_ and declare it as a string variable it executes without any errors

The function works fine until it reaches the error handling part

JavaScript

This is the output of the paste on pastebin

JavaScript

Advertisement

Answer

Your problem is calling str() on a bytes object. NEVER call str() on a bytes object to convert it to a string, since it behaves like repr(). Simply using [2:-1] will only remove the quotes but not undo escaping other special characters.

You can do this:

JavaScript

Or this:

JavaScript

Also, executing random code from the internet is an incredibly bad idea.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement