繁体   English   中英

如何找到 2 个特定的 .py 文件并运行它们

[英]How to find 2 specific .py files and run them

我在许多子文件夹中有 2 个“.py”文件。 并希望一次运行一个。 首先,我想在所有子文件夹中运行第一个,然后运行第二个。 另外,假设第一个名为 AAA,第二个名为 BBB。

下面是我的脚本。

import os
import sys

rootdir = os.getcwd()

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        #print(os.path.join(subdir, file))
        filepath = subdir + os.sep + file

        if filepath.endswith("A*.py or B*"):
            print(filepath)


os.startfile(filepath)

尝试这个:

if (filepath.endswith("A*.py" or "B*")):
    os.startfile(filepath)
    print(filepath)

if filepath.endswith("A*.py or B*"):是问题所在。 str.endswith 不是这样工作的。 这个条件应该有效:

if file == "AAA.py" or file = "BBB.py":

还要在 if 块中启动文件,所以这是完整的代码:

import os
import sys

rootdir = os.getcwd()

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        #print(os.path.join(subdir, file))
        filepath = subdir + os.sep + file

        if file == "AAA.py" or file = "BBB.py":
            print(filepath)
            os.startfile(filepath)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM