Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

im trying to make a script in python thats takes the users input and runs a file depending on that input

How do I make this work? I tried looking in websites, but to no avail, I need this to work because I want to use it instead of running the files separately, so can anyone help me with this code?

I need to know what is wrong here.

input=("input 1 for encoder and 2 for decoder: ")
if input=1 open encoder.py
if input=2 open decoder.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There are multiple ways to do this –

Method 1 –

import encoder

Note – this only works once in the entire program

Method 2 –

exec(open('decoder.py').read())

Note – this is unsafe and you should avoid when possible

Method 3 –

os.system('python file.py')

Note – this is very hacky and you shouldn’t use it much

So this should be the solution you are looking for –

import os

option = input("input 1 for encoder and 2 for decoder: ")
if option == "1":
    import encoder
elif option == "2":
    import decoder

Source – How can I make one python file run another?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading