使用return语句正确退出while循环

2024-05-16 19:36:58 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的烧瓶路径中有一个while循环。该函数的目的是从用户检索一个值,并检查str是否存在于目录中的任何文件名中。如果为true,则返回“plot_list”并退出函数。如果为false,请等待30秒,然后再次检查该str是否存在于目录中的任何文件名中

@app.route("/data")
def datatable():
    selected_project = str(request.args.get('selected_project'))
    csv_path = "..\static\csv\dist_drift"

    start_time = time.time()
    while True:
        time.sleep(5)
        for fname in os.listdir(csv_path):
            if selected_project in fname:
                print("{} csv was found in {}".format(selected_project, csv_path))
                plot_list = ["basketball.html", "hockey.html"]
                return jsonify(plot_list)

            else:
                current_time = time.time()
                print("{} csv not found in {} after waiting {} seconds!".format(selected_project, csv_path,
                                                                                (current_time - start_time)))
                time.sleep(30)

以下是函数中的打印语句:

Advanced csv was found in \static\csv\dist_drift
None csv not found in \static\csv\dist_drift after waiting 5.001329660415649 seconds!
["basketball.html", "hockey.html"]
None csv not found in \static\csv\dist_drift after waiting 35.00231146812439 seconds!
None csv not found in \static\csv\dist_drift after waiting 65.00326824188232 seconds!

我相信这是一个简单的解决办法,但我想不出来。有什么帮助吗

编辑:问题是即使str存在于目录中的文件名中,函数也没有退出

编辑2:这很有效。谢谢你的提示

@app.route("/data")
def datatable():
    selected_project = str(request.args.get('selected_project'))
    csv_path = "..\static\csv\dist_drift"

    start_time = time.time()
    time.sleep(5)
    while True:
        for fname in os.listdir(csv_path):
            if selected_project in fname:
                print("{} csv was found in {}".format(selected_project, csv_path))
                subprocess.call(
                    [r'C:\Program Files\SAS\JMP\15\jmp.exe',
                     r'C:\Users\Gol69206\PycharmProjects\rel-dashboard\JSL\Plots.jsl'])


                plot_list = ["basketball.html", "hockey.html"]
                return jsonify(plot_list)
            break
        else:
            current_time = time.time()
            print("{} csv not found in {} after waiting {} seconds!".format(selected_project, csv_path,
                                                                            (current_time - start_time)))
            time.sleep(30)

Tags: csvpathinprojecttimeplotdisthtml
2条回答

您只需要使用“break”{break是退出循环的一种简单方法} 您的代码的固定版本为:

@app.route("/data")
def datatable():
    selected_project = str(request.args.get('selected_Project'))
    csv_path = "..\static\csv\dist_drift"

    start_time = time.time()
    time.sleep(5)
    while True:
        for fname in os.listdir(csv_path):
            if selected_project in fname:
                print("{} csv was found in {}".format(selected_project , csv_path))
                plot_list = ["basketball.html", "hockey.html"]
                print(plot_list)
                break
        else:
            current_time = time.time()
            print("{} csv not found in {} after waiting {} seconds!".format(selected_project , csv_path,
                                                                            (current_time - start_time)))
            time.sleep(30)
    return jsonify(plot_list)

您的返回超出了功能范围。只需修复标签

@app.route("/data")
def datatable():
    selected_project = str(request.args.get('selected_Project'))
    csv_path = "..\static\csv\dist_drift"

    start_time = time.time()
    time.sleep(5)
    while True:
        for fname in os.listdir(csv_path):
            if selected_project in fname:
                print("{} csv was found in {}".format(selected_project , csv_path))
                plot_list = ["basketball.html", "hockey.html"]
                print(plot_list)
                return jsonify(plot_list)
        else:
            current_time = time.time()
            print("{} csv not found in {} after waiting {} seconds!".format(selected_project , csv_path,
                                                                            (current_time - start_time)))
            time.sleep(30)

相关问题 更多 >