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
>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?