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

Thursday, September 15, 2022

[FIXED] How do i run code given/taken by the user in python

 September 15, 2022     exec, kotlin, python     No comments   

Issue

I am pretty new to Kotlin, i am trying to make a project in Kotlin that executes code taken by the user. I have used exec() in python to execute code but I don't know how to use that either, please help me if you know python and Kotlin please help Any help will be very much appreciated!! Thanks

EDIT:here is some more info.. i am a newbie in Kotlin i need help with some commands which are like exec in python i am making a program to run python commands in a Kotlin program this is experimental.. forgive me as i am a newbie to Stackoverflow.. thanks..


Solution

I have an idea You can get user inputs and save them on a file. Then execute the saved file on the shell by importing 'os' library

import os

print("Enter any python commands!")
print("Enter 'run' to execute.")

user_inputs = ''

# Get user input until user enter 'run' phrase
while True:
    user_input = input('> ')
    if user_input == 'run': break
    user_inputs = user_inputs + "\r\n" + user_input

# save user input on a file
file = open("run.py", 'w')
file.writelines(user_inputs)
file.close()

# execute the code on terminal and get output, then display to the user
stream = os.popen('python run.py')
output = stream.read()

print("---------------------------------")
print(output)

Happy coding 🖥



Answered By - Peyman Majidi
Answer Checked By - Senaida (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