import subprocess # 如果当前目录下有"logs.txt"文件,先删除它 try: os.remove("logs.txt") except FileNotFoundError: pass with open("logs.txt", "w") as log_file: # 执行您的Python程序,将标准输出和标准错误重定向到/dev/null proc = subprocess.Popen(['python', 'v2_3.py'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) # 循环读取标准输出并写入日志文件 while True: output = proc.stdout.readline() if not output: break log_file.write(output.decode("utf-8")) # 等待程序执行完成 proc.wait()