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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.