2

I'm new to python, I have three python script pyapp_1.py , pyapp_2.py ,pyapp_3.py in same dir . I want to make a file like run.py where I take input from user like this:

press 1 to run pyapp_1.py 
press 2 to run pyapp_2.py 
press 3 to run pyapp_3.py

if user press 1 only pyapp_1.py should run .

how can i do this, Thanks.

3 Answers 3

3

Try this:

x=int(input("press 1 to run pyapp_1.py\npress 2 to run pyapp_2.py\npress 3 to run pyapp_3.py"))
if x==1:
    import pyapp_1
elif x==2:
    import pyapp_2
elif x==3:
    import pyapp_3
Sign up to request clarification or add additional context in comments.

Comments

2

Put you code in function, import the code into a new file like main.py

main.py

import file1.py
import file2.py
import file3.py

while true: 
  x=input()

  if (x == 1): 
     file1()
  if (x == 2):
     file2()
  if (x == 3):
     file3()

Comments

1

For running a python script example:

import runpy
runpy.run_path(path_name='pyapp_x.py')

If you use Python 3.10 or higher:

x=int(input("Please insert a number bewtween 1 and 3: "))

match x:
    case 1:
        runpy.run_path(path_name='pyapp_1.py')
    case 2: 
        runpy.run_path(path_name='pyapp_2.py')
    case 3:  
        runpy.run_path(path_name='pyapp_3.py')
    case _:
        print(f'Error: {x} is not between 1 and 3')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.