鱼C论坛

 找回密码
 立即注册

python3将help命令打印的内容保存到文件中

已有 48 次阅读2024-1-20 11:01 |个人分类:Python

需要重定向默认的标准输出流stdout
import sys

#对输出流做备份
back = sys.stdout

filepath = 'out.txt'
with open(filepath,'w') as f:
    sys.stdout = f
    help(str)

#还原输出流
sys.stdout = back



打包成函数:
import sys

def help_to_file(filepath,content):
    #对输出流做备份
    back = sys.stdout

    with open(filepath,'w') as f:
        sys.stdout = f
        help(content)

    #还原输出流
    sys.stdout = back
    
help_to_file('out.txt','str')

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-23 10:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部