读取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)