简体   繁体   中英

How to find 2 specific .py files and run them

I have 2 '.py' files in many sub-folders. And would like to run them one at a time. First, I would like to run the first one in all sub-folders and then run the second. Also, lets say the first one is named AAA and the second is BBB.

Below is my script.

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)

Try this:

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

The if filepath.endswith("A*.py or B*"): is the problem. The str.endswith doesn't work like this. This condition should work:

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

Also start the file in the if block, so this is the full code:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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