python_os.system批量读取配置文件执行程序

读取config文件夹下的config文件,多线程调用程序。(os.system方法拿不到输出的内容)

# -*- coding: UTF-8 -*-
import os
import threading
import time


def get_config(path):
    baseconfig = []
    basepath = []

    for root, dirs, files in os.walk(path):
        if 'config' in root:
            for i in files:
                if 'config' in i:
                    # 获取上层的父目录
                    basepath.append(os.path.dirname(root))
                    baseconfig.append(i)

    # print(basepath, "****", baseconfig)
    return basepath, baseconfig


def nrtkrun(root, config, nrtkspe):
    # 获取工作目录的盘符
    current_drive = os.path.splitdrive(root)[0]

    cmd1 = current_drive
    cmd2 = 'cd ' + root
    cmd3 = nrtkspe + ' config\\' + config
    print(cmd3)
    os.system('%s&&%s&&%s' % (cmd1, cmd2, cmd3))


def runtime(n):

    # path = os.getcwd()
    path = r'E:\nrtk_spe\RTK-MONITOR-20240912'

    # 设置默认的spe程序名称
    nrtkspe = 'RTK-MONITOR-20240912.exe'
    # 获取工作目录下的spe程序名称
    for file in os.walk(path):
        if 'RTK-MONITOR' in file:
            nrtkspe = file
        else:
            nrtkspe = nrtkspe

    # 获取路径和配置文件
    rc = get_config(path)
    basepath = rc[0]
    baseconfig = rc[1]

    # 调用进程批量运行
    for i in range(len(baseconfig)):
        thread = threading.Thread(target=nrtkrun, args=(basepath[i], baseconfig[i], nrtkspe))
        thread.start()

    # 运行n秒后,杀死任务
    time.sleep(n)
    # /f强制,/t
    os.system('taskkill /f /t /im ' + nrtkspe)


if __name__ == '__main__':
    while True:
        runtime(86400)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值