BBrother commited on
Commit
25322d7
1 Parent(s): 3c7def1

Update nologs.py

Browse files
Files changed (1) hide show
  1. nologs.py +16 -8
nologs.py CHANGED
@@ -1,12 +1,20 @@
 
1
  import subprocess
2
 
3
- # 执行您的Python程序并将仅标准错误重定向到subprocess.PIPE
4
- proc = subprocess.Popen(['python', 'sd_clsa_webui_colab_ipynb_v2_3.py'], stderr=subprocess.PIPE)
 
 
 
5
 
6
- # 等待程序执行完成
7
- proc.wait()
 
 
 
 
8
 
9
- # 获取程序的错误信息
10
- stderr = proc.communicate()[1]
11
-
12
- # stderr包含标准错误内容
 
1
+ import os
2
  import subprocess
3
 
4
+ try:
5
+ # 将标准输出和标准错误重定向到临时文件
6
+ with open("temp_output.txt", "w") as temp_output:
7
+ # 执行Python程序,将标准输出和标准错误重定向到临时文件
8
+ subprocess.run(["python", "sd_clsa_webui_colab_ipynb_v2_3.py"], stdout=temp_output, stderr=temp_output)
9
 
10
+ except subprocess.CalledProcessError as e:
11
+ # 如果有报错,打印错误信息
12
+ print(f"Error: {e}")
13
+ with open("temp_output.txt", "r") as temp_output:
14
+ print("Program Output:")
15
+ print(temp_output.read())
16
 
17
+ finally:
18
+ # 恢复标准输出和标准错误到默认
19
+ os.dup2(1, 1)
20
+ os.dup2(2, 2)