import subprocess import logging # 配置日志 logging.basicConfig(filename='logs.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') # 执行程序并将标准输出和标准错误重定向到日志 proc = subprocess.Popen(['python', 'v2_3.py'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) # 读取并记录标准输出和标准错误 while True: output = proc.stdout.readline() error = proc.stderr.readline() if not output and not error: break if output: logging.info(output.strip()) if error: logging.error(error.strip()) # 等待程序执行完成 proc.wait() # 关闭日志处理程序 logging.shutdown()