• 我们在哪一颗星上见过 ,以至如此相互思念 ;我们在哪一颗星上相互思念过,以至如此相互深爱
  • 我们在哪一颗星上分别 ,以至如此相互辉映 ;我们在哪一颗星上入睡 ,以至如此唤醒黎明
  • 认识世界 克服困难 洞悉所有 贴近生活 寻找珍爱 感受彼此

python: 获取外部print内容

python知识点 云涯 2年前 (2023-01-31) 746次浏览

Python3

 

import sys, io
stdout = sys.stdout
sys.stdout = io.StringIO()

# call module that calls print()
import module1
module1.test()  #另一个模块

# get output and restore sys.stdout
output = sys.stdout.getvalue()
sys.stdout = stdout

print(output)

云涯历险记 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python: 获取外部print内容
喜欢 (0)