Code file running problem

I need to save this somewhere as I find myself typing it a lot. Editing the hidden files is a totally viable option. However, if you have multiple Py files and don’t wish to manually change stuff every time you want to use one, make the main file (main.py) contain a code similar to this:

choice = int(input("Which file to run?\n1) otherFile\n2) secondFile"))
# Get user input
if choice == 1: # If user chooses option 1
  from otherFile import main # Import the function "main" from
  # "otherFile.py" in the same folder
  main() # This executes the "main" function in "otherFile.py"
elif choice == 2:
  from secondFile import main
  main()
else:
  print("Your selection is invalid. Have a nice day.") # Change this to
  # whatever you want

You can also make this into a loop and do various things to make the selection fancier, but this is just a basic example.

1 Like