PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 14, 2022

[FIXED] Why does exec raise a syntax error when given python string?

 September 14, 2022     exec, python     No comments   

Issue

I have this python code

while 1:
    exec(input())

when I enter import os \nos.system("echo 1") I get this error

  File "<string>", line 1
    import os \nos.system("echo 1")
                                  ^
SyntaxError: unexpected character after line continuation character

Solution

The problem is that you're using \n within the exec, which as @The Thonnu mentioned causes problems when parsing.

Try entering import os; os.system("echo 1") instead.

Semicolons can be used in Python to separate different lines as an alternative to semicolons.

If you must use \n in your input, you can also use:

while 1:
    exec(input().replace('\\n', '\n'))


Answered By - Krishnan Shankar
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing