0

I am very new to sw coding needing some help. Thank You!

  1. I have a script that scan a path for .py files and display it it in menu format using enumerates
  2. Ask user to select file/files they want to run and put them in a list
  3. And start ran those file from this list one by one

My issue is that it only ran the the fist selected file from the user selected list. Here is my simple code.

import os
import sys

choice_1 = ''
run_list = []
while choice_1 != 'q':
    print("\nPlease select desire test case: ")
    items = os.listdir("C:/Users/tonp\PycharmProjects/untitled1")
    fileList = [name for name in items if name.endswith(".py")]
    for cnt, fileName in enumerate(fileList, 0):
        sys.stdout.write("[%d] %s\n\r" % (cnt, fileName))
    choice = int(input("Select from [1-%s]: " % cnt))
    choice_1 = input("Press any key to add more case or q to start running testsuite ")
    run_list.append(fileList[choice])
    print("These are the case/s you have selected so far :")
    print(run_list)
    selected_files = run_list
for x in run_list:
    exec(open(c).read())
tonp
  • 1

1 Answers1

0

You may need to use importlib.

Check this link for an answer that might help you in this direction: Exit an imported module without exiting the main program - Python

To allow the user to exit as needed you can do something like this inside the while True:

sys.stdout.write('Type file name (Enter to exit): ')
try:
    sys.stdout.flush()
    filename = sys.stdin.readline().strip()
    if filename == '':
        print('Exiting...')
        break()
except:
    exit()
mhega
  • 57
  • 5
  • Thanks, partially working after finished the first file the second into infinity loop at asking for user input. – tonp Apr 13 '22 at 20:06
  • I edited the answer suggesting how to allow the user to exit out of the input so you will not end up with infinite loop – mhega Apr 13 '22 at 21:18
  • hi there, Just an update with you of what I find out. I make few python test script that simply print test1, test2, test3, etc. that seem to work fine .... every scripts executed. However, when one of the script selected have some menu with the last option from the menu to quit then the code did jump back to the original script to continue running selected scrips from the list. example script selected is test1, test2,test3 with only print statement - this work all three script ran, but when script selected is script_menu, test1, test2, test3 then the only script ran was script_menu – tonp Apr 15 '22 at 15:41
  • expecting after script_menu done when user selected exit it will continue with test1, test2, etc. – tonp Apr 15 '22 at 15:44
  • You stated "However, when one of the script selected have some menu with the last option from the menu to quit then the code did jump back to the original script". I would not tell it to exit if I am willing to continue with the same script that I am running after the last option is answered. – mhega Apr 16 '22 at 16:32
  • Depending on what you are trying to do, you may use break if you wanna break out of a loop, exit() if you wanna entirely exit out of the program, or if in some condition you want to avoid fully exiting out of the program and rather go back to a prior menu (module), the way I did this previously is I put the import_module in a "try .. except SystemExit" block, and used the exit() error code to tell the exception handler whether the error is severe enough to entirely exit or if it should rather continue with a different menu/module. Hope this helps. – mhega Apr 16 '22 at 16:33
  • GM mhega, I think you are clearly can see that I am is at a very beginner. I just one to correct one thing that I said. I mean to said the code DIDN'T jump back into the original loop. Still investigate how I would able to go back to original to executed the next one in the list when in case there is an option to quite for the first scripts. Again, appreciates very much of your time. – tonp Apr 18 '22 at 13:11
  • If you have not figured this out yet, It may help if you edit the question, put the current simplified code, and a test case scenario of the expected behavior by the same simplified code - what you are trying to implement – mhega Apr 22 '22 at 14:51