扫描一个目录创建了选定的脚本列表,放入列表中,然后一个人执行它们

发布于 2025-01-21 02:59:44 字数 860 浏览 3 评论 0原文

我对SW编码的新手需要一些帮助。谢谢你!

  1. 的菜单格式显示它
  2. 并使用枚举
  3. 有一个脚本可以扫描.py文件的路径,

我 问题是它仅从用户选择的列表中运行拳头选定的文件。这是我的简单代码。

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())

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())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘落散花 2025-01-28 02:59:44

您可能需要使用 importlib。

检查此链接以获取可能对您有帮助的答案:
退出导入的模块而不退出主模块程序 - Python

要允许用户根据需要退出,您可以在 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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文