Python代码打开多个路径并运行特定时期的.cmd文件

问题描述 投票:0回答:1

我正在尝试运行多个运行时间不同的 run.cmd 文件,考虑运行 1 文件将运行 40 秒,运行 2 文件将运行 20 秒,运行 3 文件将运行 30 秒。所以我需要的是一个程序来打开这些运行文件所在的多个路径 并在特定时间段内运行这些文件。

我尝试了一些 python 代码,但我没有得到想要的输出。

请找到下面的代码


import os 
# list of folder paths to
folders = [ r"C:\path\run1.cmd fast", 
            r"C:\path\run2.cmd fast",                  
            r"C:\path\run3.cmd fast"]  

for folder in folders:    
   os.system(folder)    
   time.sleep(5)


python python-3.x windows automation command-prompt
1个回答
0
投票
import os

# Set the parent directory
parent_directory = '/path/to/parent/directory/'

# Loop through each subdirectory within the parent directory
for subdirectory in os.listdir(parent_directory):

    # Create the full path for the subdirectory
    subdirectory_path = os.path.join(parent_directory, subdirectory)

    # Check if the subdirectory is a directory (and not a file)
    if os.path.isdir(subdirectory_path):

        # Navigate to the subdirectory
        os.chdir(subdirectory_path)

        # Run the .bat file within the subdirectory
        os.system('start my_script.bat')

        # Navigate back to the parent directory
        os.chdir(parent_directory)

对 .bat 文件尝试了同样的事情

© www.soinside.com 2019 - 2024. All rights reserved.