因此,我一直在查看模块的help()文档,但很快意识到,阅读小输出框中的文档非常麻烦。因此,因此我尝试将help()文档粘贴到另一个文件中,以使阅读更加清晰。
myfile = open("file.txt","w")
myfile.write(str(help(random)))
myfile.close()
代替编写文档,而是将其粘贴到None
中。
任何想法如何做到这一点?
答案 0 :(得分:4)
答案 1 :(得分:3)
我不建议您以这种方式阅读python文档-但这是您可以做的:您可以重定向stdout
并调用help
:
from contextlib import redirect_stdout
import random
with open('random_help.txt', 'w') as file:
with redirect_stdout(file):
help(random)
或更简单(如乔恩·克莱门茨的建议):
from pydoc import doc
import random
with open('random_help.txt', 'w') as file:
doc(random, output=file)